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

推荐订阅源

Forbes - Security
Forbes - Security
L
Lohrmann on Cybersecurity
Simon Willison's Weblog
Simon Willison's Weblog
P
Proofpoint News Feed
P
Privacy International News Feed
The Hacker News
The Hacker News
AWS News Blog
AWS News Blog
S
Securelist
P
Proofpoint News Feed
Recent Announcements
Recent Announcements
GbyAI
GbyAI
B
Blog RSS Feed
A
About on SuperTechFans
C
CXSECURITY Database RSS Feed - CXSecurity.com
Y
Y Combinator Blog
Microsoft Azure Blog
Microsoft Azure Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Cyberwarzone
Cyberwarzone
I
Intezer
T
Tor Project blog
T
The Blog of Author Tim Ferriss
The GitHub Blog
The GitHub Blog
云风的 BLOG
云风的 BLOG
Recorded Future
Recorded Future
aimingoo的专栏
aimingoo的专栏
Cisco Talos Blog
Cisco Talos Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
W
WeLiveSecurity
D
DataBreaches.Net
U
Unit 42
Project Zero
Project Zero
Martin Fowler
Martin Fowler
V
V2EX
The Last Watchdog
The Last Watchdog
Security Archives - TechRepublic
Security Archives - TechRepublic
C
Cisco Blogs
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V2EX - 技术
V2EX - 技术
Hacker News - Newest:
Hacker News - Newest: "LLM"
T
Threat Research - Cisco Blogs
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
T
Tenable Blog
F
Full Disclosure
T
The Exploit Database - CXSecurity.com
H
Heimdal Security Blog
Latest news
Latest news
Webroot Blog
Webroot Blog

博客园 - Jailu

如何在IIS5.1/IIS6.0上使用ASP.NET URL Routing Linq to SQLite使用笔记 Response.ContentType 详细列表 如何利用Google Code的SVN显示Html页面 Windows7下IIS7的安装及ASP配置方法 [原]分享几个日常开发中经常用到的软件 ASP.NET中Cookie问题求教 解决FF中的bug:JavaScript focus() throws "Permission denied to get property XULElement.selectedIndex" [原]请留心asp:Image控件中的ImageUrl属性 开心网辅助程序--开心网争车位助手正式发布(含源码) 开心网辅助程序开发手记(四):贴条功能+逻辑停车+简单界面 开心网辅助程序开发手记(三):实现停车功能 HTML:对话框插件thickbox使用技巧 - Jailu - 博客园 开心网辅助程序开发手记(二):获取好友私家车位信息 [程序收藏]把Unicode转成汉字 开心网辅助程序开发手记 原来.NET Framework 2.0也是支持母版页嵌套的 Fire event in IE & Firefox 【转】FireFox与IE开发上的一些区别
如何在Web Form中使用URL Routing?
Jailu · 2010-07-02 · via 博客园 - Jailu

什么是URL Routing?

所谓URL RoutingURL路由),指的是在Web中,URL指向的不再是某个物理文件,而是一个说明有关URL路由的字符串,开发者可以自定义该字符串的格式。在默认情况下,URL Routing在ASP.NET应用程序中是可以直接使用的,但在ASP.NET站点上,需要做一些配置才能使用。

为什么要使用URL Routing?

在使用URL Routing前,我们的URL可能是http://www.mysite.com/profile.aspx?userid=1,使用URL Routing后,我们的URL可以变成http://www.mysite.com/profile/1。修改后的URL更加友好,更有利于SEO。至于其它目的嘛,在使用过程中再慢慢挖掘吧。

URL Routing只能在MVC中才能使用吗?

路由程序集(System.Web.Routing.dll)在.NET Framework V3.5 sp1中就包含了,而MVC是在之后才发布的。因此不能说URL Routing只能在MVC中才能使用。不过在MVC中增加了Routing在一些扩展方法(包含在System.Web.MvcRouteCollectionExtemsion类中),使用起来更加方便。

下面简单介绍下如何在Web Form中使用URL Routing。

1. 添加对程序集System.Web.Abstractions.dll,System.Web.Routing.dll的引用

2. 添加一个IRouteHandler的实现类CustomRouteHanlder

CustomRouteHandler

 1 using System.Web;
 2 using System.Web.Compilation;
 3 using System.Web.Routing;
 4 using System.Web.UI;
 5 
 6 public class CustomRouteHandler : IRouteHandler
 7 {
 8     public string VirtualPath
 9     {
10         get;
11         private set;
12     }
13 
14     public CustomRouteHandler(string virtualPath)
15     {
16         this.VirtualPath = virtualPath;
17     }
18 
19     #region IRouteHandler Members
20 
21     public IHttpHandler GetHttpHandler(RequestContext requestContext)
22     {
23         var page = BuildManager.CreateInstanceFromVirtualPath(VirtualPath, typeof(Page)) as IHttpHandler;
24         return page;
25     }
26 
27     #endregion
28 }

3. 配置web.config文件

1 <httpModules>
2     <add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web.Routing, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
3 </httpModules>

4. 在Global.asax页面中注册路由表

    4.1 添加名称空间引用   

1 <%@ Import Namespace="System.Web.Routing" %>

    4.2 在Application_Start事件中注册路由表

Global.asax

1     void Application_Start(object sender, EventArgs e) 
2     {
3         RegisterRoutes(RouteTable.Routes);
4     }
5 
6     void RegisterRoutes(RouteCollection routes)
7     {
8         routes.Add("Demo1"new Route("Demo1/"new CustomRouteHandler("~/WebForm1.aspx")));
9     }

    4.3 如何设定URL Routing参数

1     void RegisterRoutes(RouteCollection routes)
2     {
3         routes.Add("Demo2"new Route("Demo2/{action}"new CustomRouteHandler("~/WebForm2.aspx")));
4     }

    4.4 如何设定URL Routing参数的黙认值

global.asax

1     void RegisterRoutes(RouteCollection routes)
2     {
3         routes.Add("Demo3"new Route("Demo3/{action}"new RouteValueDictionary { { "action""show" } }, new CustomRouteHandler("~/WebForm3.aspx")));
4     }

    4.5 如何在路由目标页面使用URL里的参数

         4.5.1 修改自定义类CustomRouteHanlder

CustomRouteHandler

        public IHttpHandler GetHttpHandler(RequestContext requestContext)
        {
            
// 从ReqestContext对象中获取URL参数,并把值写到HttpContext的Items集合中供路由目标页面使用
            foreach (var urlParm in requestContext.RouteData.Values)
            {
                requestContext.HttpContext.Items[urlParm.Key] 
= urlParm.Value;
            }

            var page 

= BuildManager.CreateInstanceFromVirtualPath(VirtualPath, typeof(Page)) as IHttpHandler;
            
return page;
        }

        4.5.2 在路由目标页面中读取URL参数

<%= this.Context.Items["action"%>

    4.6 如何为URL参数添加约束

global.asax

1     void RegisterRoutes(RouteCollection routes)
2     {
3         routes.Add("Demo4"new Route("Demo4/{action}"new RouteValueDictionary { { "action""show" } }, new RouteValueDictionary { { "action"@"\w{4}" } }, new CustomRouteHandler("~/WebForm4.aspx")));
4     }

    如代码所示,要求action参数值必须是4个字符,若action参数长度不等于4个字符,则会得到“无法找到资源”的错误提示。

点击下载示例代码