惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Help Net Security
Help Net Security
G
Google Developers Blog
雷峰网
雷峰网
WordPress大学
WordPress大学
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Engineering at Meta
Engineering at Meta
Security Latest
Security Latest
T
Threat Research - Cisco Blogs
AWS News Blog
AWS News Blog
F
Full Disclosure
C
Cybersecurity and Infrastructure Security Agency CISA
T
The Exploit Database - CXSecurity.com
J
Java Code Geeks
U
Unit 42
C
Cyber Attacks, Cyber Crime and Cyber Security
V
V2EX
C
Cisco Blogs
博客园 - 司徒正美
Project Zero
Project Zero
L
LINUX DO - 热门话题
阮一峰的网络日志
阮一峰的网络日志
Blog — PlanetScale
Blog — PlanetScale
Scott Helme
Scott Helme
A
About on SuperTechFans
Hugging Face - Blog
Hugging Face - Blog
S
Securelist
小众软件
小众软件
aimingoo的专栏
aimingoo的专栏
S
Schneier on Security
G
GRAHAM CLULEY
酷 壳 – CoolShell
酷 壳 – CoolShell
Cyberwarzone
Cyberwarzone
MongoDB | Blog
MongoDB | Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 叶小钗
T
Threatpost
Recorded Future
Recorded Future
C
CXSECURITY Database RSS Feed - CXSecurity.com
宝玉的分享
宝玉的分享
N
News and Events Feed by Topic
人人都是产品经理
人人都是产品经理
The Register - Security
The Register - Security
S
Security Archives - TechRepublic
博客园 - Franky
N
News | PayPal Newsroom
Simon Willison's Weblog
Simon Willison's Weblog
S
SegmentFault 最新的问题
W
WeLiveSecurity
A
Arctic Wolf
B
Blog

博客园 - 8user

partial关键字的含义和使用 JS仿Open()打开一个新窗口,并不是弹出新窗口 判断是否含有汉字 - 8user - 博客园 如何统计代码行执行的时间? - 8user - 博客园 时间的正则表达式验证 深入探讨IsPostBack【转】 IIS无法启动的解决方法 FCKEditor的使用及配置方法 C#中的值类型和引用类型 C#在线备份数据库至制定目录 - 8user - 博客园 c#实现天气预报查询 C#中的枚举器 C#里面比较时间大小三种方法 - 8user - 博客园 c#中删除ArrayList中的重复数据 - 8user - 博客园 SQL Server数据库开发的二十一条规则 c#实现程序的开机启动 - 8user VS2008 快捷键大全 - 8user C#调用dos命令 反射性能测试 - 8user
Cool MVC: 使用正则式限定路由规则Rount - 8user
8user · 2009-06-10 · via 博客园 - 8user
这里列出一些基本的关于MVC路由规则的使用正则表达式的例子。
            /*Front*/
            //限定id只能是数字, 长度为0~11
            routes.MapRoute(
                "Archive",
                "{user}/Archive/{id}",
                new { controller = "Blog", action = "Archive", user = "", id = 0 },
                new { id = @"[\d]{0,11}" }//new { id = @"[\d]*" }//*表示任意长度
            );

            //限定page只能是数字, 长度为0~8
            routes.MapRoute(
                "Archives",
                "{user}/Page/{page}",
                new { controller = "Blog", action = "Archives", page=1 },
                new { page = @"[\d]{0,8}" }
            );

            /*Default*/
            //限定controller只能匹配  Home,Blog,About,Admin  
            routes.MapRoute(
                "Default",                                              // Route name
                "{controller}/{action}/{id}",                           // URL with parameters
                new { controller = "Home", action = "Index", id = "" }, // Parameter defaults
                new { controller =@"Home|Blog|About|Admin"}
            );

            /*Blog*/
            //限定user的长度是1~50
            routes.MapRoute(
                "Blog",
                "{user}",
                new { controller = "Blog", action = "Archives", user = "Home" },
                new { user = @"[\w]{1,50}" }
            ); 
 
测试:如果用户在浏览器输入http://localhost/bruce/Archive/1 则匹配第一个规则,正确;如果输入http://localhost/bruce/Archive/0123456789120000 或者http://localhost/bruce/Archive/abc 则会显示没有找到对应网页。简单实用的正则表达式!
 
关于正则表达式可以参考这里学习:http://www.regexlab.com/zh/regref.htmhttp://zh.wikipedia.org/wiki/%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F

作者:Bruce 编程的艺术世界出处:http://coolcode.cnblogs.com 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。