Question

In my routeConfig I added a custom route on the top of default route

routes.MapRoute(
    name: "appointmentandattendee",
    url: "{controller}/{action}/{appointmentId}/{attendeeId}",
    defaults: new { controller = "Response", action = "Index", appointmentId = UrlParameter.Optional, attendeeId = UrlParameter.Optional }
);

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

So, for this action

public ActionResult Index(Guid appointmentId, Guid attendeeId)
{
   return Content("Hello");
}

routing looks like

http://localhost/Response/Index/96de7851-49f6-4b69-8a58-2bea39bd466e/7d4fe8ed-7dae-e311-be8f-001c42aef0b2

but now the routing with name Default is ignored

for example: In my Appointment controller with some action and parameter as id I was expecting Default route to be used, but it doesn't So, for this action in AppointmentController

public ActionResult Test(Guid id)
{          
    return Content("Hello");
}

routing looks like

localhost:/Appointment/Test?id=96de7851-49f6-4b69-8a58-2bea39bd466e

Why is that? Shouldn't it be using the default routing in this case?

Was it helpful?

Solution

Because your route appointmentandattendee overrides your default route.

try this:

routes.MapRoute(
    name: "appointmentandattendee",
    url: "Response/{action}/{appointmentId}/{attendeeId}",
    defaults: new { controller = "Response", action = "Index", appointmentId = UrlParameter.Optional, attendeeId = UrlParameter.Optional }
);

I specified URL with first part as Response, default controller remains controller = "Response". Now your route not as general as it was before.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top