Cool MVC: use regular expressions to limit routing rules Rount

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.