Tutorial by Examples

Enabling Attribute Routing To enable attribute routing, call MapMvcAttributeRoutes during configuration. public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute(“{resource}.axd/{*pathInfo}”); routes.MapMvcAttributeRoutes(); routes.MapRoute( name: “Default”, ...
You can make a URI parameter optional by adding a question mark to the route parameter. You can also specify a default value by using the form parameter=value. public class BooksController : Controller { // eg: /books // eg: /books/1430210079 [Route(“books/{isbn?}”)] public Act...
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(i...
You can also apply the [Route] attribute on the controller level, capturing the action as a parameter. That route would then be applied on all actions in the controller, unless a specific [Route] has been defined on a specific action, overriding the default set on the controller. [RoutePrefix(“prom...
Route constraints let you restrict how the parameters in the route template are matched. The general syntax is {parameter:constraint}. For example: // eg: /users/5 [Route(“users/{id:int}”] public ActionResult GetUserById(int id) { … } // eg: users/ken [Route(“users/{name}”] public ActionRes...

Page 1 of 1