I searched around for my problem, but could not find a solution…
I have a BlogController, and I want to match the following routes to a separate action:
/blog/
/blog/rss
/blog/tags/tagName
However, I want to match any other url’s, like:
/blog/my-post
/blog/other-post
to the Post action.
I tried with
routes.MapRoute("Blog",
"blog/{action}/{param}",
new { controller = "Blog", action = "Index", param = UrlParameter.Optional });
routes.MapRoute("BlogPost",
"blog/{slug}",
new { controller = "Blog", action = "post" });
but the second route is never matched.
Any ideas?
The first route allready match any urls in the form of
blog/slug.Whe resolving routes, ASP.NET MVC will try use the first match, and even if there is no action to get that method. ASP.NET MVC’s routing still won’t try the next route.
So with your routes, the url
blog/my-first-articlewill match the first url and MVC will look for themy-first-actionmethod on theBlogControllerclass.Solution 1
You could either define seperate routes for each method, like this:
Solution 2
You could use a constraint to define valid values for
{action}in the first route.The constraint is in form of a regex.