星空网 > 软件开发 > ASP.net

UrlRewrite(URL重写)

概述

今天看了下URL重写的实现,主要看的是MS 的URL Rewrite。

URL重写的优点有:更友好的URL,支持老版本的URL

URL重写的缺点有:最主要的缺点是性能低下,因为如果要支持无后缀的URL(但更多的情况是我们要支持这种方式)就必须在IIS中配置所有的URL(包括js,css,image)都要转发到aspnet_isapi中,解决方法可以参见 慎用url重写;还有一个性能问题是,根据源代码,在匹配url时,用正则表达式尝试匹配每一个规则,直至有一个匹配成功,或都匹配不成功才结束。那么那些不需要重写的URL,就要将所有的正则表达式都要执行一次,可以在进入匹配之前先做一个判断,排除掉一些情况

1 配置 web.config

1.1 下载 MS的URLRewrite

地址是:http://download.microsoft.com/download/0/4/6/0463611e-a3f9-490d-a08c-877a83b797cf/MSDNURLRewriting.msi

1.2 配置自定义section的声明节点

 <configSections>  <section name="RewriterConfig" type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter" /> </configSections>

1.3 配置自定义section的内容

用UrlRewrite在完成配置后,主要的工作就是写配置url映射的正则表达式了。对于正则式不清楚的人,可以看看这个正则表达式入门及备忘

 <RewriterConfig>  <Rules>   <RewriterRule>    <LookFor>~/pick/?</LookFor>    <SendTo><![CDATA[~/pick.aspx]]></SendTo>   </RewriterRule>   <RewriterRule>    <LookFor>~/pick/(\d+)</LookFor>    <SendTo><![CDATA[~/pick.aspx?page=$1]]></SendTo>   </RewriterRule>   <RewriterRule>    <LookFor>~/(\w+)/p/(\d+).html</LookFor>    <SendTo><![CDATA[~/BlogDetails.aspx?blogwriter=$1&blogid=$2]]></SendTo>   </RewriterRule>   <RewriterRule>    <LookFor>~/Product/(\w{4}).html</LookFor>    <SendTo>~/Product.aspx?id=$1</SendTo>   </RewriterRule>  </Rules> </RewriterConfig>

1.4 配置httpmodules或httphandlers

在如果是旧的IIS 6.0,在<system.web>节点下配置,httpmodules和httphandlers只需要一个就好,用于拦截所有请求

  <httpModules>    <add type="URLRewriter.ModuleRewriter, URLRewriter" name="ModuleRewriter" />  </httpModules>    <!--<httpHandlers>    <add verb="*" path="*.aspx" type="URLRewriter.RewriterFactoryHandler, URLRewriter" />  </httpHandlers>-->

在新的IIS7.0中, 在<system.webServer>节点下配置, modules 和handlers同样只需要一个

 <system.webServer>  <modules>   <add type="URLRewriter.ModuleRewriter, URLRewriter" name="ModuleRewriter" />  </modules>  <!--<handlers>   <add verb="*" path="*.*" type="URLRewriter.RewriterFactoryHandler, URLRewriter" name="URLRewriter" />  </handlers>--> </system.webServer>

1.5 配置IIS

在发布到IIS后,如果访问路径出错,需要做一下扩展名的映射。额,我用的IIS7.0是没有报错,直接就可访问了。

2 代码分析

UrlRewrite的源代码十分的简单,实际上就是实现了一个IHttpModule的接口来拦截所有的请求URL。然后,将请求的URL用正则表达式与每一个匹配,直到有一个匹配成功,则将原有的url(假的)根据正则表达式转成实际的url。如果都匹配不成功,则按原有的路径处理请求

主要代码是

UrlRewrite(URL重写)UrlRewrite(URL重写)
protected override void Rewrite(string requestedPath, System.Web.HttpApplication app)    {      // log information to the Trace object.      app.Context.Trace.Write("ModuleRewriter", "Entering ModuleRewriter");      // get the configuration rules      RewriterRuleCollection rules = RewriterConfiguration.GetConfig().Rules;      // iterate through each rule...      for(int i = 0; i < rules.Count; i++)      {        // get the pattern to look for, and Resolve the Url (convert ~ into the appropriate directory)        string lookFor = "^" + RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, rules[i].LookFor) + "$";        // Create a regex (note that IgnoreCase is set...)        Regex re = new Regex(lookFor, RegexOptions.IgnoreCase);        // See if a match is found        if (re.IsMatch(requestedPath))        {          // match found - do any replacement needed          string sendToUrl = RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, re.Replace(requestedPath, rules[i].SendTo));          // log rewriting information to the Trace object          app.Context.Trace.Write("ModuleRewriter", "Rewriting URL to " + sendToUrl);          // Rewrite the URL          RewriterUtils.RewriteUrl(app.Context, sendToUrl);          break;    // exit the for loop        }      }      // Log information to the Trace object      app.Context.Trace.Write("ModuleRewriter", "Exiting ModuleRewriter");    }

View Code

看了这个源代码,还学到的一个东西就是在web.config中自定义节点,然后实现一下IConfigurationSectionHandler 接口,将section的内容转成对象,在程序中使用,主要代码有:

UrlRewrite(URL重写)UrlRewrite(URL重写)
    public static RewriterConfiguration GetConfig()    {      if (HttpContext.Current.Cache["RewriterConfig"] == null)        HttpContext.Current.Cache.Insert("RewriterConfig", ConfigurationManager.GetSection("RewriterConfig"));            return (RewriterConfiguration) HttpContext.Current.Cache["RewriterConfig"];    }  /// <summary>  /// Deserializes the markup in Web.config into an instance of the <see cref="RewriterConfiguration"/> class.  /// </summary>  public class RewriterConfigSerializerSectionHandler : IConfigurationSectionHandler   {    /// <summary>    /// Creates an instance of the <see cref="RewriterConfiguration"/> class. : IConfigurationSectionHandler     /// </summary>    /// <remarks>Uses /// <see cref="RewriterConfiguration"/> instance.</remarks>    /// <returns>An instance of the <see cref="RewriterConfiguration"/> class.</returns>    public object Create(object parent, object configContext, System.// Create an instance of       new typeof(RewriterConfiguration));      // Return the Deserialized object from the Web.config       return ser.Deserialize(new 

View Code

3 最后

最后就是,UrlReWrite虽然好用,但是根据这源代码,性能问题是个大头,但平常自己的小网站用用还是没问题的,如果请求量大了,也知道这里UrlRewrite这里有问题,可以用httphander的方式只拦截特定的url,也可以用IIS filter去搞(没搞过,但应该能用,毕竟是在IIS端拦截大部分的请求,而不是将求放到扩展程序中去)。关于IIS filter可以参考这个文章  在 ASP.NET 中执行 URL 重写

 




原标题:UrlRewrite(URL重写)

关键词:URL

URL
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

如何查询vat的EORI-如何查询vat税号-如何查询vat证书文件:https://www.kjdsnews.com/a/1303701.html
阿联酋vat可以自己注册吗-阿联酋官方网站-阿联酋注册公司:https://www.kjdsnews.com/a/1303702.html
法国vat税率多少-德国vat税率多少-法国vat税号:https://www.kjdsnews.com/a/1303703.html
亚马逊欧洲vat政策-法国vat亚马逊强制要求-亚马逊意大利vat严不严:https://www.kjdsnews.com/a/1303704.html
中国vat-进口vat-vat服务商:https://www.kjdsnews.com/a/1303705.html
澳洲vat红酒好不好-澳洲vat508-澳洲vat807:https://www.kjdsnews.com/a/1303706.html
皇家游轮航线 皇家邮轮旅游攻略:https://www.vstour.cn/a/408245.html
2017春节旅游攻略有吗:https://www.vstour.cn/a/408246.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流