gqinvs 发表于 2015-11-15 08:04:21

WINDOWS SERVER 2003、IIS6、ASP.NET2.0,用微软的UrlRewriter组件重写URL

  一、配置IIS6
  在IIS下配置“应用程序映射”,也就是对于不同的扩展名(.php、.html、.asp、.aspx等)都有谁来处理
  这里我们选择“通配符应用程序映射”,也就是把所有扩展名,包括没有扩展名的,或者以/结尾的url,都发送到ASP.NET来处理。
  ASP.NET处理这些文件的程序,也就是“可执行文件”是:C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/aspnet_isapi.dll,这个文件。

  确认文件是否存在不要选,因为url重写后很多情况都是没有对应文件的。
  
  
  二、在自己的网站中引用微软的UrlRewriter组件
  

  微软的URL重写类在这里下载,下载后只需编译里面的URLRewriter类为DLL文件,然后在你的项目中做个引用就可以了。
http://www.microsoft.com/china/msdn/library/webservices/asp.net/URLRewriting.mspx
  
  三、在网站的web.config文件里注册UrlRewriter组件并添加改写规则
  
  <configSections><section name=&quot;RewriterConfig&quot; type=&quot;URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter&quot; /></configSections><system.web><httpModules><add name=&quot;ScriptModule&quot; type=&quot;System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35&quot;/><!-- 用于 URL 重写 --><add type=&quot;URLRewriter.ModuleRewriter, URLRewriter&quot; name=&quot;ModuleRewriter&quot; /><!-- 用于 URL 重写 --></httpModules></system.web>
  
  web.config里面有两部分配置用于注册UrlRewriter,一部分在 configSections 里面,一部分在 System.Web 里面
  
  然后就是在 web.config 里面配置重写规则,可以用正则表达式来匹配改写后的url,发现匹配(lookfor)后,转到(SendTo)原始url
  <RewriterConfig><Rules><RewriterRule><LookFor>(.+)/(.+).html</LookFor><SendTo>~/$2.aspx</SendTo></RewriterRule><RewriterRule><LookFor>~/(/d{4})/(/d{2})/(/d{2})/.aspx</LookFor><SendTo>~/ShowBlogContent.aspx?year=$1&month=$2&day=$3</SendTo></RewriterRule></Rules></RewriterConfig>
  
  四、处理回发
  
  访问一个被重写了的url,而这个url上面有一个窗体,虽然打开窗体的时候,使用的是重写前的url,但是当点击窗体的提交按钮的时候,窗体会被定向到重写后的实际的url去。
具体的实现方法就是“利用ASP.NET 2.0控件适配器扩展架构来定制控件的输出”,说白了就是ASP.NET 2.0提供了一个功能,可以手工的改写服务器控件。那么我们可以改写form控件的窗体的action,也就是提交路径的属性。
  需要两个文件:
  1、
  App_Browsers/Form.browser<browsers><browser refID= &quot;Default&quot; ><controlAdapters><adapter controlType= &quot;System.Web.UI.HtmlControls.HtmlForm&quot;adapterType= &quot;FormRewriterControlAdapter&quot;/></controlAdapters></browser></browsers>
  
  
  App_Code/FormRewriter.csusingSystem.Data;usingSystem.Configuration;usingSystem.Web;usingSystem.Web.Security;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;usingSystem.Web.UI.WebControls.WebParts;usingSystem.Web.UI.HtmlControls;/// <summary>/// FormRewriter 的摘要说明/// </summary>public   classFormRewriterControlAdapter : System.Web.UI.Adapters.ControlAdapter{publicFormRewriterControlAdapter(){}protected   override   voidRender(HtmlTextWriter writer){base .Render( newRewriteFormHtmlTextWriter(writer));}}public   classRewriteFormHtmlTextWriter : HtmlTextWriter{publicRewriteFormHtmlTextWriter(HtmlTextWriter writer):base (writer){base .InnerWriter = writer.InnerWriter;}publicRewriteFormHtmlTextWriter(System.IO.TextWriter writer):base (writer){base .InnerWriter = writer;}public   override   voidWriteAttribute( stringname,stringvalue,boolfEncode){//If the attribute we are writing is the &quot;action&quot; attribute, and we are not on a sub-control,   //then replace the value to write with the raw URL of the request - which ensures that we'll//preserve the PathInfo value on postback scenarios if(name ==&quot;action&quot; ){HttpContext context = HttpContext.Current;if(context.Items[ &quot;ActionAlreadyWritten&quot; ] ==null ){//We will use the Request.RawUrl property within ASP.NET to retrieve the origional   //URL before it was re-written. value = context.Request.RawUrl;//Indicate that we've already rewritten the &ltform>'s action attribute to prevent//us from rewriting a sub-control under the &ltform> controlcontext.Items[ &quot;ActionAlreadyWritten&quot; ] =true ;}}base .WriteAttribute(name, value, fEncode);}}
  
  URL重写就可以在 Windows Server 2003 + IIS6 + ASP.NET 2.0 下实现了
             版权声明:本文为博主原创文章,未经博主允许不得转载。
页: [1]
查看完整版本: WINDOWS SERVER 2003、IIS6、ASP.NET2.0,用微软的UrlRewriter组件重写URL