问题
On latest ASP.NET MVC CORE 2 default template, i am trying to change default action by modifying controller and action as below. I am expecting to see login page as default but i am having 404 http error. What I am doing wrong ?
You can verify this issue on default ASP.NET CORE 2 MVC project.
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Account}/{action=Login}/{id?}");
});
}
回答1:
If you look at the AccountController
class, you'll see it's decorated with a Route
attribute, like so:
[Route("[controller]/[action]")]
public class AccountController : Controller
However, if you look at the HomeController
class, you'll see that it is not decorated with such an attribute:
public class HomeController : Controller
Because the AccountController
is using Attribute routing, it will not be picked up using the Conventional routing template. The docs explain this mutual exclusivity:
Actions that define attribute routes cannot be reached through the conventional routes and vice-versa.
来源:https://stackoverflow.com/questions/47713292/changing-default-action-doesnt-work-for-asp-net-core-2