【问题标题】:Unnecessary Dotnet Core MVC "Home" and "Index" URL components?不必要的 Dotnet Core MVC“主页”和“索引”URL 组件?
【发布时间】:2018-03-11 20:22:26
【问题描述】:

Dotnet Core 2.0 MVC 的默认路由和 URL 生成会产生非常奇怪的 URL。我不记得他们在 ASP.Net MVC 中是这样的。

即使用 Dotnet Core MVC 模板创建新站点时,我们看到生成了以下 URL:

/Home/About « 我可以发誓这曾经只是/About

/Manage/Index«这绝对是/Manage

在第一个示例中,这与当前的 ASP.NET MVC(.NET 模板)模板相同。我不记得在 URL 中看到“主页”。这似乎很奇怪。为什么不只是 /About?

在第二个示例中,ASP.NET MVC(.NET 模板)和 Dotnet Core 之间存在差异。最后的“索引”显然是多余的。

有人知道这里有什么吗?有没有一种简单的方法来更改默认路由定义来清理它?

【问题讨论】:

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


【解决方案1】:

看看属性路由,你可以用它来以这种方式声明你的路由:

public class HomeController : Controller
{
   [Route("")]
   [Route("Home")]
   [Route("Home/Index")]
   public IActionResult Index()
   {
      return View();
   }
   [Route("Home/About")]
   public IActionResult About()
   {
      return View();
   }
   [Route("Home/Contact")]
   public IActionResult Contact()
   {
      return View();
   }
}

https://docs.microsoft.com/en-us/aspnet/core/mvc/controllers/routing#attribute-routing

【讨论】:

    猜你喜欢
    • 2020-04-14
    • 1970-01-01
    • 1970-01-01
    • 2019-03-26
    • 2017-12-27
    • 2012-09-08
    • 2019-05-26
    • 2019-12-02
    • 1970-01-01
    相关资源
    最近更新 更多