webform,如何在WebForm中使用Url Routing之说明

今天看到一个问题提出的有关在webForm 下使用System.we.Routing实现url重写的设想,特花了些时间整理了下,将如下作为解答;
要明白如何使用url Routing先搞清楚以下问题:
什么是URL Routing?
所谓URL Routing(URL路由),指的是在Web中,URL指向的不再是某个物理文件,而是一个说明有关URL路由的字符串,开发者可以自定义该字符串的格式。在默认情况下,URL Routing在ASP.NET应用程序中是可以直接使用的,但在ASP.NET站点上,需要做一些配置才能使用
为什么要使用URL Routing?
在使用URL Routing前,我们的URL可能是http://www.website.com/Index.aspx?id=1,使用URL Routing后,我们的URL可以变成http://www.website.com/Index/1。修改后的URL更加友好,更有利于SEO。
URL Routing只能在MVC中才能使用吗?
路由程序集(System.Web.Routing.dll)在.NET Framework V3.5 sp1中就包含了,而MVC是在之后才发布的。因此不能说URL Routing只能在MVC中才能使用。不过在MVC中增加了Routing在一些扩展方法(包含在System.Web.Mvc的RouteCollectionExtemsion类中),使用起来更加方便.
下面简单介绍下如何在Web Form中使用URL Routing。
1. 添加对程序集System.Web.Abstractions.dll,System.Web.Routing.dll的引用.
2. 添加一个IRouteHandler的实现类WebFormRouteHandler
代码如下:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Routing; using System.Web.Compilation; using System.Web.UI;
/// ///WebFormRouteHandler 的摘要说明 /// public class WebFormRouteHandler:IRouteHandler { /// /// 具体呈现的页面路径 /// public string VirtualPath { get;
private set; } public WebFormRouteHandler(string virtualpath) { this.VirtualPath = virtualpath; } #region IRouteHandler 成员 /// /// 实现 IHttpHandler接口中的GetHttpHandler方法, /// ///
/// 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; }
#endregion }
3. 修改配置web.config文件(必要的支持url Routing方法)


4、 Global.asax页面代码如下(如果没有则添加此文件) <%@ Application Language="C#" %> <%@ Import Namespace="System.Web.Routing" %> public static void RegisterRoutes(RouteCollection routes) { //注意此处调用的是WebFormRouteHandler类 routes.Add("Named", new Route("NameIndex/{action}", new RouteValueDictionary { { "action", "NameIndex" } }, new WebFormRouteHandler("~/Named/Index.aspx"))); routes.Add("Numbers", new Route("NumberIndex/{action}", new RouteValueDictionary { { "action", "NumberIndex" } }, new WebFormRouteHandler("~/Numbers/Index.aspx"))); //添加验证,要求action参数值必须是13个字符,若action参数长度不等于13个字符(酌情更改),则会得到“无法找到资源”的错误提示。 routes.Add("Validate",new Route("ValidateIndex/{action}",new RouteValueDictionary { { "action", "ValidateIndex" }},new RouteValueDictionary { { "action", @"\w{13}" } },new WebFormRouteHandler("~/Validate/Index.aspx"))); }
void Application_Start(object sender, EventArgs e) { RegisterRoutes(System.Web.Routing.RouteTable.Routes); } void Application_End(object sender, EventArgs e) { //在应用程序关闭时运行的代码
} void Application_Error(object sender, EventArgs e) { //在出现未处理的错误时运行的代码
}
void Session_Start(object sender, EventArgs e) { //在新会话启动时运行的代码
}
void Session_End(object sender, EventArgs e) { //在会话结束时运行的代码。 // 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为 // InProc 时,才会引发 Session_End 事件。如果会话模式 //设置为 StateServer 或 SQLServer,则不会引发该事件。
}
1、Named(文件夹)
下面包含一个文件:Index.aspx
2、Numbers(文件夹)
下面包含一个文件:Index.aspx
3、Validate(文件夹)
下面包含一个文件:Index.aspx
现在要将项目的根目录下的Default.aspx文件稍作修改
添加如下内容:
Go To Named/Index
Go To Numbers/Index
Go To ValidateIndex!
所有的代码都写完了,下面进行调试,如果没有问题,代表程序可以跑 :)
最后运行程序,点击Default.aspx页面中的三个链接,看看效果 :)
最后说明:程序中包含了参数的使用,参数的验证等,
如果此程序同学们有什么更好的建议,请说明,如果您认为哪里有问题,请不要出口伤人,出来混,总是要还的 :) 如果有需要,本人会上传源码,欢迎同学们踊跃参与讨论;

Tags: 

延伸阅读

最新评论

发表评论