I am trying to do what SO does for its Question controller.
/Posts/{id}/{title}when viewing a post (action name not shown)/Posts/Newwhen you are posting something new./Posts/Delete/10etc….
I have two routes set up (well, one if you don’t count the default). What appears to be happening is all actions in the Post controller are being routed through the first one.
What is that? I obviously have it wrong, but I can’t figure this out.
routes.MapRoute("ViewPosts",
"Posts/{postid}/{title}",
new { controller = "Posts", action = "View", postid = "", title = "" });
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
The first route handles all your requests which starts from
/Posts.You need to use constraints to allow
{postid}be only number:In this case only if numeric Id is provided this route will handle it, otherwise “Default” route will handle.