Search code examples
asp.netasp.net-mvc-routing

.NET MVC 4.5 rewrite index route


Currently I have a controller like this:

class AccountController : Controller {

    public ActionResult Index() {
        return View();
    }
}

It's ok except one part that user when go to this page will have this url: http://myhost/account/index, which is unwanted. How can I have http://myhost/account only ?


Solution

  • Something like this could do the trick

    routes.MapRoute(
        name: "Account",
        url: "account/{action}/{id}",
        defaults: new { controller = "Account", action = "Index", id = UrlParameter.Optional }
    );
    

    It could be also done with Attribute routing decorating with [Route] the method you want to be the default.