[英]Changing default action doesn't work for ASP.NET Core 2
On latest ASP.NET MVC CORE 2 default template, i am trying to change default action by modifying controller and action as below. 在最新的ASP.NET MVC CORE 2默认模板上,我试图通过修改控制器和操作来更改默认操作,如下所示。 I am expecting to see login page as default but i am having 404 http error.
我希望看到登录页面为默认,但我有404 http错误。 What I am doing wrong ?
我做错了什么?
You can verify this issue on default ASP.NET CORE 2 MVC project. 您可以在默认的ASP.NET CORE 2 MVC项目上验证此问题。
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Account}/{action=Login}/{id?}");
});
}
If you look at the AccountController
class, you'll see it's decorated with a Route
attribute, like so: 如果您查看
AccountController
类,您将看到它使用Route
属性进行修饰,如下所示:
[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: 但是,如果你看一下
HomeController
类,你会发现它没有用这样的属性装饰:
public class HomeController : Controller
Because the AccountController
is using Attribute routing , it will not be picked up using the Conventional routing template. 由于
AccountController
使用的是属性路由 ,因此不会使用常规路由模板来获取它。 The docs explain this mutual exclusivity: 文档解释了这种互斥性:
Actions that define attribute routes cannot be reached through the conventional routes and vice-versa.
定义属性路由的动作不能通过传统路径到达,反之亦然。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.