For default route using attribute routing with route prefix you need to set the route template as an empty string. You can also override the site root using ~/
if the controller already has a route prefix.
[RoutePrefix("Zome")]
public class HomeController : Controller {
[HttpGet]
[Route("", Name = "Zndex")] //Matches GET /Zome
[Route("Zndex")] //Matches GET /Zome/Zndex
[Route("~/", Name = "default")] //Matches GET / <-- site root
public ActionResult Index() {
return View();
}
//...
}
That said, when using attribute routing on a controller it no longer matches convention-based routes. The controller is either all attribute-based or all convention-based that do not mix.
Reference Attribute Routing in ASP.NET MVC 5
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…