Asp.net Url改写方法——使用Routing实现

使用Routing实现

 

系列文章目录

一、伪静态方式(使用URLRewriter)
     1、改写方法    (文章地址:http://www.cnblogs.com/scottckt/archive/2011/01/12/1933737.html)
     2、回发时处理方法(文章地址:http://www.cnblogs.com/scottckt/archive/2011/01/12/1933791.html)
     3、将Aspx改写成HTml方法(文章地址:http://www.cnblogs.com/scottckt/archive/2011/01/12/1933836.html)

二、真实改写(使用System.Web.Routing)(文章地址:http://www.cnblogs.com/scottckt/archive/2011/01/12/1933893.html)


 

 

一:引入 System.Web.Routing 程序集

  System.Web.Routing程序集.Net Framework3.5中。Add Reference,引入之后,在 Web.Config 的 <assemblies> 中可以看到添加了一行,如果没有,请手工添加。
< add  assembly ="System.Web.Routing, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /></ assemblies >

二:加入 UrlRoutingModule 配置
System.Web.Routing 是基于 httpModules 来实现的,所以还应该在 Web.Config 的 <httpModules> 节点中加上:
< add  name ="UrlRoutingModule"  type ="System.Web.Routing.UrlRoutingModule, System.Web.Routing, Version=3.5.0.0, 

Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>



三:在程序启动时注册 Url 路由规则

  这要在 Global.asax 中控制,在 Application_Start() 方法中注册路由条目。如果你理解 TCP/IP 网络的路由就很容易理解这里的路由,其实是一个道理。TCP/IP 网络的路由说的是数据包要去往哪里应该经过哪个网关,而这里的路由是指要访问哪个实际的资源应该走哪个 IRouteHandler 实例。

Global.asax 的注册路由的代码如下:

 

  using System.Web.Routing;
using
 Useful;
        
protected   void  Application_Start( object  sender, EventArgs e)
        {
            RegisterRoutes(RouteTable.Routes);
        }

        
#region  自定义方法
        
///   <summary>
        
///  注册Url路由
        
///   </summary>
        
///   <param name="routes"></param>
        
///  Test: http://localhost :5876/test-11查看结果
         private   void  RegisterRoutes(RouteCollection routes)
        {
            IRouteHandler routerHander 
=   new  RssRouteHandler();
            Route DealedRoute 
=   null ;
            
// 对于 /type-num 这种规则的 Url 会转到 rssRouteHandler 实例来处理 
            DealedRoute  =   new  Route( " {type}-{num} " , routerHander);
            routes.Add(DealedRoute);
        }
        
#endregion

 


  Url规则与URL Rewriter中的规则类似。可能以是一般的方式也可以用正则表示。 。符合规则的 Url 会转到与它相关联的 IRouteHandler 来处理。注意其中的 RouteValueDictionary 参数分别代表着什么,以及它们的声明办法,怎么设定正则表达式,以及与参数的对应。注册路由时还可以附加其他的约束条件,比如只处理 POST 请求等等。


四:IRouteHandler 实现
     前面既然是为 Url 映射 IRouteHandler 实现,那么这里来看看 RssRouteHandler 是长什么样子的。RssRouteHandler 类我放在了 Useful项目中。
using  System.Web.Routing;
 
namespace  Useful
{
    
public   class  RssRouteHandler : IRouteHandler
    {
        
public  RssRouteHandler()
        {
        }
        
public  System.Web.IHttpHandler GetHttpHandler(RequestContext requestContext)
        {
            
return   new  RssProvider(requestContext);            
        }
    }
}

    只要实现 IRouteHandler 接口,实现接口方法即可,接口方法需要返回的是一个 IHttpHandler 实现实例。并且参数 RequestContext 是在你的 IHttpHandler 实现类是用得着的。这样路由完成了从 Url -> IRouteHandler -> IHttpHandler 的层进,写过 HttpHandler 的人应该知道它可以处理页面能做的事情。那么来看看这里的 RssProvider 的简单实现吧:

五:IHttpHandler 实现
using  System;
using  System.Web;
 
namespace  Useful
{
    
public   class  RssProvider : IHttpHandler
    {
        
public  System.Web.Routing.RequestContext RequestContext {  get set ; }
        
private   bool  HaveParamter  =   false ;
        
public  RssProvider(System.Web.Routing.RequestContext context)
        {
            
this .RequestContext  =  context;
        }
 
        
public   bool  IsReusable
        {
            
get
            {
                
return   false ;
            }
        }
 
        
public   void  ProcessRequest(HttpContext context)
        {
            DealParamter(context);
        }
 
        
private   void  DealParamter(HttpContext context)
        {
            
string  type  =   this .RequestContext.RouteData.Values[ " type " ].ToString();
            
int  num  =  Int32.Parse( this .RequestContext.RouteData.Values[ " num " ].ToString());
            context.Server.Execute(
" Test.aspx?type= "   +  type  +   " &num= "   +  num);
            context.Response.Write(
" Type: "   +  type  +   " <br/>Number: "   +  num  +   " <br/> " );
            context.Response.Write(
" 根据上面的条件设置响应头,输出需要的数据 " );
        }
 
    }
}

     可以从重写的 Url 中按照规则获得请求参数,然后转给实际的资源去处理,或者直接在 HttpHandler 的 ProcessRequest() 方法中或再调用其他方法来玩成本次请求。
    在 RssRouteHandler 为 RssProvider 这个 HttpHandler 实现类注入了 RequestContext  实例,你可以用这个实例从 Url 中获得想要的参数。还有,在实际应用中最好能明确 IRouteHandler 和 IHttpHandler 实现类实例的复用情况,可选择适当的设计模式来保证。

六:如果是在 Win2003 的 IIS 6,配置其他未知请求类型也由 Asp.Net 来处理
     在 XP 下的 IIS 6 或是任何平台的 IIS 7 都不需要作特别的设置。Default Web Site 的属性,Home Directory 标签页里,Configuation...,为 Wildcard application maps(order of implementation) Insert 一项,指向到 c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll,不勾选 Verify that file exists.

     这样就完成了,通过 /type-num等规则就会执行到 RssProvider  的 ProcessRequest() 方法中,这样就完成了真实 Url 的重写,也能实现不需要 aspx 文件处理 Http 请求。

 

 

Codeplex有个例子与此例相似,但对一些地方作了封装。下载地址: http://aspnet35routing.codeplex.com/SourceControl/list/changesets

 

参考文章:http://school.ty502.com/wz_4563.html

你可能感兴趣的:(asp.net)