Special MVC Routing

I have an asp.net MVC project where I need to define some custom routes. Similar to what you see for posts in Wordpress, where the route has the form postid-postname:

12-i-am-post

I know how to do something like postid/postname:

12/i-am-post.

But how do I make a route that combines two, for example:

mywebsite.com/12-postname-is-her
+3
source share
2 answers
routes.MapRoute(
    "PostRoute", // Route name
    "{controller}/{id}-{postName}", // URL with parameters
    new { controller = "Home", action = "Index", id = UrlParameter.Optional, postName = UrlParameter.Optional } // Parameter defaults
);


public ActionResult Index(int id, string postName)
{
    return View();
}

Should work for the following request http: // localhost / Post / 1-MyPostName

+3
source

, /, , . catchall {*path} .

0

All Articles