Map route asp.net mvc

VirtuoZ picture VirtuoZ · Jun 30, 2013 · Viewed 18.6k times · Source

I'm trying to make my url seo friendly. I need to make url with this structure

www.domainname.com/article123.

And with this route

routes.MapRoute(
        "articlename", // Route name
        "aaaa/{articleID}", // URL with parameters
         new {action="DetailsByName",controller="Article"},
        new string[] { "bssnew.Controllers" } // Parameter defaults);

It doesn't work. MY route link looks like this

 @Html.RouteLink("aaa ","articlename", new {articleID="CentralPark",},new { @class = "item-link" })

But when I add controller and action in route it works

 routes.MapRoute(
        "articlename", // Route name
        "aaaa/{controller}/{action}/{articleID}", // URL with parameters
         new {action="DetailsByName",controller="Article"},
        new string[] { "bssnew.Controllers" } // Parameter defaults);

Answer

James picture James · Jun 30, 2013

If you need an ID only specific route, the following should work

routes.MapRoute(
    "articlename", // Route name
    "{articleID}", // URL with parameters
    new { action="DetailsByName",controller="Article" }, // parameter defaults 
    new[] { "bssnew.Controllers" } // controller namespaces
);

Assuming you have a controller which looks like

namespace bssnew.Controllers
{
    public class Article : Controller
    {
        public ActionResult DetailsByName(string articleID)
        {
            ...
        }
    }
}