MVC Route Attribute 查找多个控制器类型
MVC Route Attribute finds multiple controller types
我有 2 条匹配的路由,但我需要它们都按原样匹配。
我不能使用 GUID 或 INT,我需要它是字符串。
这条路线只有在它不符合任何其他条件时才应该被采用。
[Route("{slug}", Order = 1)]
这条路线应该一直走
[Route("eula", Order = 0)]
关于如何实现这一点有什么想法吗?
我认为订购可能有效,但显然无效
显示我的确切问题:
Route parameters and multiple controller types
重现错误:
RouteConfig.cs :
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
控制器:
public class HomeController : Controller
{
[Route("eula")]
public ActionResult Eula()
{
ViewBag.Message = "Your application description page.";
return View();
}
}
public class SlugController : Controller
{
[Route("{slug}")]
public ActionResult SluggedUrl(string slug)
{
ViewBag.Message = "Your " + slug;
return View();
}
}
Slug 控制器路由过于笼统。 Order
属性也适用于同一控制器中的操作。
建议:为 slug 添加基于约定的路由
public static void RegisterRoutes(RouteCollection routes) {
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
//catch all route
routes.MapRoute(
name: "Slug",
url: "{*slug}",
defaults: new { controller = "Slug", action = "SluggedUrl"}
);
}
并移除属性路由形式SlugController
public class SlugController : Controller {
public ActionResult SluggedUrl(string slug) {
ViewBag.Message = "Your " + slug;
return View();
}
}
这样更具体的 eula
属性路由将在更通用的 slug
路由
之前得到匹配
我有 2 条匹配的路由,但我需要它们都按原样匹配。 我不能使用 GUID 或 INT,我需要它是字符串。
这条路线只有在它不符合任何其他条件时才应该被采用。
[Route("{slug}", Order = 1)]
这条路线应该一直走
[Route("eula", Order = 0)]
关于如何实现这一点有什么想法吗? 我认为订购可能有效,但显然无效
显示我的确切问题: Route parameters and multiple controller types
重现错误:
RouteConfig.cs :
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
控制器:
public class HomeController : Controller
{
[Route("eula")]
public ActionResult Eula()
{
ViewBag.Message = "Your application description page.";
return View();
}
}
public class SlugController : Controller
{
[Route("{slug}")]
public ActionResult SluggedUrl(string slug)
{
ViewBag.Message = "Your " + slug;
return View();
}
}
Slug 控制器路由过于笼统。 Order
属性也适用于同一控制器中的操作。
建议:为 slug 添加基于约定的路由
public static void RegisterRoutes(RouteCollection routes) {
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
//catch all route
routes.MapRoute(
name: "Slug",
url: "{*slug}",
defaults: new { controller = "Slug", action = "SluggedUrl"}
);
}
并移除属性路由形式SlugController
public class SlugController : Controller {
public ActionResult SluggedUrl(string slug) {
ViewBag.Message = "Your " + slug;
return View();
}
}
这样更具体的 eula
属性路由将在更通用的 slug
路由