Meca
Meca

Reputation: 148

how to have a single controller and action handle all routes in asp.net core?

I have these paths:

 /r/dog 
 /r/dog/one 
 /r/dog/one/two 
 /r/dog/one/two/three

The path could go on like this pattern forever.

I want just one controller and one action to handle all these requests. How do I configure an ASP.NET Core route to handle this requirement?

I tried these instructions but they didn't work.

Tried this:

routes.MapRoute("r", "/r/{action}", new {controller = "Dashboard", action="Index" });

Upvotes: 3

Views: 2655

Answers (1)

Narendra
Narendra

Reputation: 1412

check out this link: https://msdn.microsoft.com/en-us/library/cc668201.aspx. There is a section called: Handling a Variable Number of Segments in a URL Pattern

You need to annotate your controller and action to match the following pattern: query/{queryname}/{*queryvalues}

For example

    [Route("r")]
    public sealed class PageController : Controller
     {
    [Route("{*queryvalues}")]
     public IActionResult Index()
        { 
          return View();

        }
      }

Upvotes: 4

Related Questions