【发布时间】:2017-09-14 04:43:34
【问题描述】:
如果我将 [Route(Name = "WhatEver")] 应用于操作,我将其用作默认站点路由,则在访问站点根目录时会收到 HTTP 404。
例如:
- 创建新的示例 MVC 项目。
-
添加属性路由:
// file: App_Start/RouteConfig.cs public class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapMvcAttributeRoutes(); // Add this line routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } ); } } -
添加路由属性
[RoutePrefix("Zome")] public class HomeController : Controller { [Route(Name = "Zndex")] public ActionResult Index() { return View(); } ... }
现在,当您启动项目进行调试时,您将遇到 HTTP 错误 404。我应该如何将属性路由与默认路由映射一起使用?
【问题讨论】:
-
为
Index操作使用[Route(Name = "Zndex")]属性的目的是什么?[domain]/Zome将自动引用[domain]/Zome/Index,使用RouteAttribute可能需要[domain]/Zome/Zndex才能到达Index操作。 -
我认为路由名称没有用于路由匹配,所以
[domain]/Zome/Zndex指向无处。我需要索引操作上的路由名称,因为我想使用带有如下路由名称的 Url 助手:<a href="@Url.RouteUrl("Zndex")">Zndex page</a>
标签: c# asp.net-mvc asp.net-mvc-5 asp.net-mvc-routing