Question

This is my custom route

// custom route
routes.MapRoute(
    "Custom",
    "Town/{townName}/{restaurantID}",
    new { controller = "Town", action = "View", restaurantID = UrlParameter.Optional }
);

Problem is when I click on Create action URL is (as it should be) Town/Create but site recognizes Create as townName which is problem... Same thing is happening with Edit.

Thanks.

Was it helpful?

Solution

Unfortunately you are dealing with 2 very generic routes. Whichever is ordered first would have precedence, but neither ordering would satisfy what you want. The easiest way to fix this is use a constraint or to specify additional routes for Create and Edit.

Here is what it would look like to create additional routes to make Create and Edit explicit.

Note: Keep in mind that with this URL structure you can never have a town named "Edit" or "Create". Fortunately, these towns do not seem to exist on the earth (yet), but there is the town of Délété to worry about: http://nona.net/features/map/placedetail.2381031/D%C3%A9l%C3%A9t%C3%A9/

routes.MapRoute(
    "Town_Edit",
    "Town/Edit/{restaurantID}",
    new { controller = "Town", action = "Edit" }
);

routes.MapRoute(
    "Town_Create",
    "Town/Create/",
    new { controller = "Town", action = "Create" }
);

routes.MapRoute(
    "Town",
    "Town/{townName}/{restaurantID}",
    new { controller = "Town", action = "View", restaurantID = UrlParameter.Optional }
);

// Default route here
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top