Often, the routes in a controller all start with the same prefix. For example:
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) { … }
}
You can set a common prefix for an entire controller by using the [RoutePrefix] attribute:
[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) { … }
}
Use a tilde (~) on the method attribute to override the route prefix if needed:
[RoutePrefix(“reviews”)]
public class ReviewsController : Controller
{
// eg.: /spotlight-review
[Route(“~/spotlight-review”)]
public ActionResult ShowSpotlight() { … }
…
}