gpt4 book ai didi

c# - 更改默认操作对 ASP.NET Core 2 不起作用

转载 作者:行者123 更新时间:2023-11-30 14:22:55 27 4
gpt4 key购买 nike

在最新的 ASP.NET MVC CORE 2 默认模板上,我试图通过如下修改 Controller 和操作来更改默认操作。我希望看到默认登录页面,但出现 404 http 错误。我做错了什么?

您可以在默认的 ASP.NET CORE 2 MVC 项目上验证此问题。

app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Account}/{action=Login}/{id?}");
});
}

最佳答案

如果您查看 AccountController 类,您会看到它装饰有 Route 属性,如下所示:

[Route("[controller]/[action]")]
public class AccountController : Controller

但是,如果您查看 HomeController 类,您会发现它没有用这样的属性修饰:

public class HomeController : Controller

因为 AccountController 正在使用 Attribute routing , 它不会被拾取使用 Conventional routing模板。 docs解释这种互斥性:

Actions that define attribute routes cannot be reached through the conventional routes and vice-versa.

关于c# - 更改默认操作对 ASP.NET Core 2 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47713292/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com