我想在MVC 5中設置屬性路由。我有一個默認行爲,但行動根本不在我的模板中。這是我的舊路線。MVC 5屬性路由 - 默認行動不在模板中
routes.MapRoute(null,
"Article/{id}/{*path}",
new { controller = "Article", action = "Index" , id = UrlParameter.Optional, path = UrlParameter.Optional }
);
這將滿足/文章/ 1 /測試的路線,而無需使用所述索引中的路徑(即/文章/索引/ 1 /測試)的作用。
我也有另一種方法,將被默認的地圖路徑捕獲.... /文章/項目。
我的問題是我無法弄清楚如何在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()
{
}
}
可以肯定的是,你想爲你的第一個動作寫'Article/1/test',爲第二個寫'Items/Items'呢? –