bigopon bigopon - 18 days ago 4
ASP.NET (C#) Question

.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 ?

Answer

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.