Search code examples
c#asp.net-mvcasp.net-mvc-routing

What should be an MVC route for this URL?


I need to define an MVC route for URL like this:

http://localhost/RealSuiteApps/RealHelp/-1/Detail/BRK18482020

where:

Detail - is controller name

  • default action Index should be executed
  • -1 is some client id
  • BRK18482020 is orderId

I need this to go to DetailController, Index action with orderId parameter.

I tried this:

routes.MapRoute(
    name: "Detail",
    url: "Detail/{id}",
    defaults: new { clientid = "-1", controller = "Detail", action = "Index", id = UrlParameter.Optional }
);

but I get a message "Page Not Found". What am I missing here ??


Solution

  • Assuming DetailController action

    public ActionResult Index(int clientId, string orderId) { ... }
    

    Then route would be mapped as

    routes.MapRoute(
        name: "Detail",
        url: "{cientId}/Detail/{orderId}",
        defaults: new { clientid = "-1", controller = "Detail", action = "Index" }
    );
    

    Note that this should also be registered before any default routes.