我有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();
}
}
答案 0 :(得分:1)
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
路线之前匹配