路由前缀
通常,控制器中的路由都以相同的前缀开头。例如:
public class ReviewsController : Controller
{
// eg: /reviews
[Route(`reviews`)]
public ActionResult Index() { … }
// eg: /reviews/5
[Route(“reviews/{reviewId}”)]
public ActionResult Show(int reviewId) { … }
// eg: /reviews/5/edit
[Route(“reviews/{reviewId}/edit”)]
public ActionResult Edit(int reviewId) { … }
}
你可以使用[RoutePrefix]属性为整个控制器设置公共前缀:
[RoutePrefix(`reviews`)]
public class ReviewsController : Controller
{
// eg.: /reviews
[Route]
public ActionResult Index() { … }
// eg.: /reviews/5
[Route(“{reviewId}”)]
public ActionResult Show(int reviewId) { … }
// eg.: /reviews/5/edit
[Route(“{reviewId}/edit”)]
public ActionResult Edit(int reviewId) { … }
}
如果需要,在 method 属性上使用波浪号(〜)覆盖路由前缀:
[RoutePrefix(`reviews`)]
public class ReviewsController : Controller
{
// eg.: /spotlight-review
[Route(“~/spotlight-review”)]
public ActionResult ShowSpotlight() { … }
…
}