Here are some basic examples of using regular expressions for MVC routing rules.
/* Front * // The specified id can only be a number and the length is 0 ~ 11 routes. mapRoute ("Archive", "{user}/Archive/{id}", new {controller = "Blog", action = "Archive", user = "", id = 0 }, new {id = @ "[\ d] {0, 11}"} // new {id = @ "[\ d] *"} // * indicates any length ); // The page can only be a number with a length of 0 ~ 8 routes. mapRoute ("Archives", "{user}/Page/{page}", new {controller = "Blog", action = "Archives", page = 1 }, new {page = @ "[\ d] {0, 8}"});/* Default * // the controller can only match with Home, Blog, About, and Admin routes. mapRoute ("Default", // Route name "{controller}/{action}/{id}", // URL with parameters new {controller = "Home ", action = "Index", id = ""}, // Parameter defaults new {controller = @ "Home | Blog | Abo Ut | Admin "});/* Blog * // set the user length to 1 ~ 50 routes. mapRoute ("Blog", "{user}", new {controller = "Blog", action = "Archives", user = "Home "}, new {user = @ "[\ w] {1, 50 }"});
Test: If you enter http: // localhost/bruce/Archive/1 in the browser, the first rule is matched. If you enter http: // localhost/bruce/Archive/0123456789120000 or http: // localhost/bruce/Archive/abc, the corresponding webpage is not found. Simple and practical regular expression!
Learn about regular expressions here: http://www.regexlab.com/zh/regref.htm or http://zh.wikipedia.org/wiki/%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F