ASP.NET MVC Route Contraints with {ID}-{Slug} Format
- by TimLeung
I have a route like following, ideally I would like it to match:
domain.com/layout/1-slug-is-the-name-of-the-page
routes.MapRoute(
"Layout", // Route name
"layout/{id}-{slug}", // URL with parameters
new { controller = "Home", action = "Index", id = @"\d+$" }
);
But when I hit the url, I am keep on getting this exception:
The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult Index(Int32)' in ....
The above route will match the following though:
domain.com/layout/1-slug or domain.com/layout/1-slug_permalink
Seems like the hyphen that separates the ID from the Slug is causing issues.