路由字首
通常,控制器中的路由都以相同的字首開頭。例如:
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() { … }
…
}