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

推荐订阅源

GbyAI
GbyAI
Simon Willison's Weblog
Simon Willison's Weblog
Microsoft Security Blog
Microsoft Security Blog
Y
Y Combinator Blog
The GitHub Blog
The GitHub Blog
Engineering at Meta
Engineering at Meta
F
Fortinet All Blogs
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
A
About on SuperTechFans
Last Week in AI
Last Week in AI
月光博客
月光博客
有赞技术团队
有赞技术团队
P
Proofpoint News Feed
MyScale Blog
MyScale Blog
Martin Fowler
Martin Fowler
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
C
Check Point Blog
U
Unit 42
The Register - Security
The Register - Security
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Hugging Face - Blog
Hugging Face - Blog
阮一峰的网络日志
阮一峰的网络日志
V
Visual Studio Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
D
DataBreaches.Net
WordPress大学
WordPress大学
aimingoo的专栏
aimingoo的专栏
H
Hacker News: Front Page
Recent Announcements
Recent Announcements
C
CXSECURITY Database RSS Feed - CXSecurity.com
Latest news
Latest news
小众软件
小众软件
P
Palo Alto Networks Blog
PCI Perspectives
PCI Perspectives
Security Latest
Security Latest
S
Secure Thoughts
Scott Helme
Scott Helme
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
T
Threat Research - Cisco Blogs
P
Proofpoint News Feed
M
MIT News - Artificial intelligence
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Google DeepMind News
Google DeepMind News
Recorded Future
Recorded Future
O
OpenAI News
S
Securelist
云风的 BLOG
云风的 BLOG
H
Help Net Security
T
Troy Hunt's Blog

博客园 - Jun1st

Application之间共享Master Page ASP.NET AJAX 4的Client-Side Template和DataView ASP.NET AJAX 4的Client-Side Template和DataView 体验ASP.NET4之ClientID 使用Extension Methods来使IDataReader更加方便 Custom WCF Configuration File USE HttpRuntime.Cache OVER HttpContext.Current.Cache Make Asynchronous Calls from Page IIS and VS Embedded Local Web Server Integrate jQuery with ASP.NET Data Controls Tech-Ed 2008 上海 ASP.NET MVC的分页和导航 LINQ and Pipeline Pattern Why Would a .Net Programmer Learn Ruby On Rails(翻译) ASP.NET MVC之AJAX Asp.Net MVC---Walkthrough Asp.Net MVC 入门篇——Overview Asp.Net 2.0之SqlCacheDependency Security Basics and ASP.NET Support(翻译)
体验ASP.NET 4之URL Routing
Jun1st · 2009-10-25 · via 博客园 - Jun1st

2009-10-25 00:15  Jun1st  阅读(4094)  评论()    收藏  举报

什么是URL Routing

首先,URL routing其实在ASP.NET 3.5 SP1中就被引入了,但是介于之前一直没有用到,况且微软的很多关于ASP.NET 4的介绍都把这作为一新的功能,因此就把题目取为这个吧。
之前,一个典型的ASP.NET的URL通常如下

                                            http://www.myexample.com/salesreport.aspx?year=2009

在这个URL中的salesreport.aspx代表的是一个真实存在的物理文件,后缀.aspx在url中的存在不仅完全没有意义,而且使得这个url不是SEO-friendly的。而使用URL Routing之后,我们就可使用如下更简洁明了的地址来访问了,

                                          http://www.myexample.com/salesrepot/2009

使用Route Engine进行URL Mapping

在asp.net mvc中,通过MapRoute将url映射到相应的controller和action, 而在web form中,在Global.assx的Application_Start中,我们通过MapPageRoute来将url映射到一个页面上

1.void RegisterRoutes(RouteCollection routes)

2.{

3.    routes.MapPageRoute("SalesRoute", "SalesReport/{year}", "~/sales.aspx");                               

4.}

MapPageRoute这里使用了三个参数,第一个是给这个Route的命名,第二个是这个URL的映射Pattern, 而最后一个就是对应的ASPX页面。除了这个最常用直接的方法之外,还可以使用其重载的其它方法,来个route设置default值,添加各种约束条件,如下面的这一个

01.void RegisterRoutes(RouteCollection routes)

02.{

03.    routes.MapPageRoute("SalesRoute",

04.                        "SalesRoute/{year}",

05.                        "~/sales.aspx", true,

06.                        new RouteValueDictionary{

07.                            { "year", DateTime.Now.Year.ToString()}},                                         

08.                        new RouteValueDictionary{

09.                            { "year", @"\d{4}" } });

10.}

相比于第一个route,这个多了三个参数,第一个为boolean值,后面两个为RouteValueDictionary, 其中第一个就指定了一个default值,而最后一个就是一个约束,通过正则表达式约束了参数year必须是四位的数字。

在将SalesRoute/2009成功的映射到sales.aspx页面之后,如何在sales的code中获取这个“2009"值呢?通过Page类的RouteData来获取

1.protected void Page_Load(object sender, EventArgs e)

2.{

3.    string year = RouteData.Values["year"] as string;                                                      

4.}

使用Routing Engine生成URL

除了解析URL之外,我们同时可以生成这些简洁明了的URL, 如下面的代码

1.RouteValueDictionary parameters = new RouteValueDictionary

2.{

3.    { "year", "2008" },

4.{ "category", "recreation" }

5.};  

7.VirtualPathData vpd = RouteTable.Routes.GetVirtualPath(null, "SalesRoute", parameters);                   

8.hyperLnk.NavigateUrl = vpd.VirtualPath;

这里的SalesRoute就是我们在Application_Start中注册过的那个Route。 值得注意的是,在SalesRoute的Pattern中,我们并没有”{category}”,那么RouteEngine怎么处理这个值呢?这时,category就为被作为一个querystring添加到url中,生成的URL将会是这样的:

                                          http://www.myexample.com/salesreport/2009?category=recreation

此外,如果在这个sales页面上加一个button,这个button对应的url会不会也会如上面的url那样简洁呢?答案是肯定的。但是是通过在Form标签中,指定Action来实现页面的回发。

总结

虽然这个新生添加Route功能并不能使Asp.Net Web Form变得像Asp.Net MVC那样让人心动,不过这至少是一个不错的功能。

Hope the post helps. Thanks