【发布时间】:2014-03-15 14:19:03
【问题描述】:
我在我的控制器中使用来自 MVC5 的属性路由。
问题:
有没有办法控制控制器之间的属性路由优先级?
考虑以下
[Route("home/{action=index}/{username?}")]
public class HomeController : Controller
{
[Route("home/index/{username?}", Order = 1)]
[Route("home/{username?}", Order = 2)]
[Route("{username?}", Order = 3)]
public ActionResult Index()
{
// ... bunch of stuff
}
}
基于上面的代码,HomeController.Index() action 方法应该使用以下请求来调用:
- 域/
- 域/{用户名}
- 域/家/
- 域/家/{用户名}
- 域/家/索引/
- 域/家/索引/{用户名}
第二控制器:
[Authorize(Roles = "Member")]
[Route("profile/{action=index}")]
public class ProfileController : Controller
{
[Route("profile")]
public ActionResult Index()
{
}
}
应使用以下请求调用ProfileController.Index()。
- 域/配置文件
- 域/配置文件/索引
问题
从示例中,如果我在 url 中发送 domain/profile,则会引发歧义异常。 domain/{username} 和 domain/profile 之间似乎存在歧义。
现在,如果我使用基于约定的路由,这将有效(第一场比赛获胜)。但它可以在 MVC5 属性路由中完成吗?因为我发现第三方库支持控制器之间的优先级
https://github.com/mccalltd/AttributeRouting/wiki/Controlling-Route-Precedence
routes.MapAttributeRoutes(config =>
{
config.AddRoutesFromController<ProfileController>();
config.AddRoutesFromController<HomeController>();
});
【问题讨论】:
标签: asp.net-mvc-routing asp.net-mvc-5 attributerouting