Changing default action doesn't work for ASP.NET Core 2

Deadly 提交于 2019-12-23 22:50:16

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!