Global.asax
or
- public class MvcApplication : System.Web.HttpApplication
- {
- protected void Application_Start()
- {
- // Enable attribute route
- RouteTable.Routes.MapMvcAttributeRoutes();
- AreaRegistration.RegisterAllAreas();
- RouteConfig.RegisterRoutes(RouteTable.Routes);
- }
- }
RouteConfig.cs
Direct route
- public class RouteConfig
- {
- public static void RegisterRoutes(RouteCollection routes)
- {
- routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
- // Enable attribute route
- routes.MapMvcAttributeRoutes();
- AreaRegistration.RegisterAllAreas();
- routes.MapRoute(
- name: "Default",
- url: "{controller}/{action}/{id}",
- defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
- );
- }
- }
Optional paramter
- public class BookController : Controller {
- // Map to 'http://prhythm.com.tw/Pet/10'
- [Route("Pet/{id}")]
- public ActionResult Pet(int? id) {
- return View();
- }
- }
Default parameter value
- public class BookController : Controller {
- // Optional parameter id
- // Map to 'http://prhythm.com.tw/Pet'
- // or 'http://prhythm.com.tw/Pet/5'
- [Route("Pet/{id?}")]
- public ActionResult Pet(int? id) {
- return View();
- }
- }
Paramter constrains
- public class BookController : Controller {
- // Default value of id
- // Map to 'http://prhythm.com.tw/Pet'
- // or 'http://prhythm.com.tw/Pet/10'
- [Route("Pet/{id=10}")]
- public ActionResult Pet(int? id) {
- return View();
- }
- }
- public class BookController : Controller {
- // Paramters constrains
- [Route("Pet/{id:int}")]
- public ActionResult Pet(int? id) {
- return View();
- }
- }
Constraint | Description | Example |
---|---|---|
alpha | Matches uppercase or lowercase Latin alphabet characters (a-z, A-Z) | {x:alpha} |
bool | Matches a Boolean value. | {x:bool} |
datetime | Matches a DateTime value. | {x:datetime} |
decimal | Matches a decimal value. | {x:decimal} |
double | Matches a 64-bit floating-point value. | {x:double} |
float | Matches a 32-bit floating-point value. | {x:float} |
guid | Matches a GUID value. | {x:guid} |
int | Matches a 32-bit integer value. | {x:int} |
length | Matches a string with the specified length or within a specified range of lengths. | {x:length(6)} {x:length(1,20)} |
long | Matches a 64-bit integer value. | {x:long} |
max | Matches an integer with a maximum value. | {x:max(10)} |
maxlength | Matches a string with a maximum length. | {x:maxlength(10)} |
min | Matches an integer with a minimum value. | {x:min(10)} |
minlength | Matches a string with a minimum length. | {x:minlength(10)} |
range | Matches an integer within a range of values. | {x:range(10,50)} |
regex | Matches a regular expression. | {x:regex(^\d{3}-\d{3}-\d{4}$)} |
Override route prefix
- [RoutePrefix("Booking")]
- public class BookController : Controller {
- // Map to 'http://prhythm.com.tw/Booking'
- // or 'http://prhythm.com.tw/Booking/Pet'
- [Route]
- [Route("Pet")]
- public ActionResult Pet() {
- return View();
- }
- // Map to 'http://prhythm.com.tw/Booking/Edit'
- [Route("Edit")
- public ActionResult Edit() {
- return View();
- }
- }
Default route
- [RoutePrefix("Booking")]
- public class BookController : Controller {
- // Override route prefix
- // Map to 'http://prhythm.com.tw/Pet'
- [Route("~/Pet")]
- public ActionResult Pet() {
- return View();
- }
- }
Route area
- [RoutePrefix("Booking")]
- [Route("action=pet")]
- public class BookController : Controller {
- // Map to 'http://prhythm.com.tw/Booking'
- // or 'http://prhythm.com.tw/Booking/Pet'
- public ActionResult Pet() {
- return View();
- }
- // Map to 'http://prhythm.com.tw/Booking/Edit'
- public ActionResult Edit() {
- return View();
- }
- }
- [RouteArea("Backend")]
- [RoutePrefix("News")]
- public class NewsController : Controller {
- // Map to 'http://prhythm.com.tw/backend/news'
- public ActionResult Index() {
- return View();
- }
- }
- [RouteArea("Backend", AreaPrefix = "Admin")]
- [RoutePrefix("News")]
- public class NewsController : Controller {
- // Map to 'http://prhythm.com.tw/admin/news'
- public ActionResult Index() {
- return View();
- }
- }
Reference http://sampathloku.blogspot.tw/2013/11/attribute-routing-with-aspnet-mvc-5.html
Reference http://blogs.msdn.com/b/webdev/archive/2013/10/17/attribute-routing-in-asp-net-mvc-5.aspx