我正在尝试在 MVC 5 中设置属性路由。我有一个具有默认操作的路由,但该操作根本不在我的模板中。这是我的旧路线。
routes.MapRoute(null,
"Article/{id}/{*path}",
new { controller = "Article", action = "Index" , id = UrlParameter.Optional, path = UrlParameter.Optional }
);
这将满足/Articles/1/test 的路由,而不必为路径中的操作使用索引(即/Articles/Index/1/test)。
我还有另一种方法将被默认地图路线捕获..../Article/Items.
我的问题是我无法弄清楚如何使用 MVC 5 中的新属性路由设置此路由。下面的代码类型有效,但我不想为每个方法添加路由。我希望在类的顶部放置一些可以满足所有方法的东西,而我只是在我的 Index 方法中添加其他东西。
[RoutePrefix("Article")]
public class ArticleController: BaseController
{
[Route("{id?}/{*path?}")]
public ActionResult Index(int id, string path)
{
}
[Route("Items")]
public ActionResult Items()
{
}
[Route("TestMethod")]
public ActionResult TestMethod()
{
}
}
我希望得到类似下面的东西。问题是我被迫将索引放在我的路径前面,就像这样.../Index/1/test.
[RoutePrefix("Article")]
[Route("{action=Index}")]
public class ArticleController : BaseController
{
Route[("{id?}/{*path?}")]
public ActionResult Index(int id, string path)
{
}
public ActionResult Articles()
{
}
public ActionResult TestMethod()
{
}
}