【问题标题】:MVC 6 - Areas Routing is not redirecting to the configured pageMVC 6 - 区域路由未重定向到配置的页面
【发布时间】:2017-08-30 18:09:45
【问题描述】:

我正在研究“Microsoft.AspNetCore.Mvc”:“1.1.2”

我的项目文件夹结构如下

startup.cs 中的代码如下

当我运行我的项目时,我没有被重定向到索引页面。它显示 404 错误。 我是否缺少任何设置?

【问题讨论】:

  • 我发现这个答案已经解决了,你试试this

标签: asp.net-core asp.net-core-mvc


【解决方案1】:

你的 url 应该有这个模式(由于你的路由配置):

http://localhost:your_port_number/area_name

当您运行您的应用程序时,您的 url 模式是:

http://localhost:your_port_number

url中没有区域名称。

你应该在url中指定区域Admin,因为你没有默认区域并且你得到404 Not Found,所以你的url应该是这样的:

http://localhost:your_port_number/Admin

您可以在app.UseMvc() 中为Area 设置默认值:

routes.MapRoute("adminRoute", "{area=Admin}/{controller=Admin}/{action=Index}/{id?}");

现在默认情况下,您使用控制器名称 Admin 和操作名称 Index 调用区域 Admin 中的端点。

【讨论】:

  • 在 app.UseMVC() 中设置区域名称对我有用。现在我的页面按预期重定向到 Index.cshtml。我还删除了控制器上的 [Area("Admin")]。我对我的问题进行了很多搜索,但从未在 MapRoute 中获得区域名称。现在我想知道如果不在 MapRoute 中指定区域名称,它对其他人如何工作。
  • 是的 :) 非常感谢!
猜你喜欢
  • 2020-05-26
  • 1970-01-01
  • 2012-07-22
  • 1970-01-01
  • 1970-01-01
  • 2012-01-31
  • 2018-12-29
  • 2011-05-01
  • 2021-06-08
相关资源
最近更新 更多