URL重寫是截取傳入Web 請求並自動將請求重定向到其他 URL 的過程。
比如:瀏覽器發來請求 http://localhost:90/URLRewriter/1.html ,
服務器自動將這個請求中定向爲http://localhost:90/URLRewriter/url.aspx?id=1
URLRewriter下載編譯後提取其中的URLRewriter.dll和ActionlessForm.dll
一、URL重寫
項目引用URLRewriter.dll
web.config配置:
<?xml version="1.0"?>
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
<configSections>
<section name="RewriterConfig" type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter" />
</configSections>
<RewriterConfig>
<Rules>
<RewriterRule>
<LookFor>~/URLRewriter/(.[0-9]*)\.html</LookFor>
<SendTo>~/URLRewriter/url.aspx?id=$1</SendTo>
</RewriterRule>
<RewriterRule>
<LookFor>~/web</LookFor>
<SendTo>~/URLRewriter/url.aspx</SendTo>
</RewriterRule>
</Rules>
</RewriterConfig>
<system.web>
<httpHandlers>
<!--URLRewriter begin 使用 HTTP 處理程序執行重寫-->
<!--<add verb="*" path="*.aspx" type="URLRewriter.RewriterFactoryHandler, URLRewriter" />
<add verb="*" path="*.html" type="URLRewriter.RewriterFactoryHandler, URLRewriter" />-->
<!--URLRewriter end-->
</httpHandlers>
<httpModules>
<!--URLRewriter begin 使用 HTTP 模塊執行重寫 -->
<add type="URLRewriter.ModuleRewriter, URLRewriter" name="ModuleRewriter"/>
<!--URLRewriter end-->
</httpModules>
</system.web>
</configuration>
IIS配置:
網站--屬性--主目錄--配置--插入--C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll
將文件是否存在 勾去掉
二、處理PostBack回發
ActionlessForm.dll以重寫Form的方式用於處理PostBack回發後URL變爲原始地址
項目引用ActionlessForm.dll在頁面中註冊一下
<%@ Register TagPrefix="skm" Namespace="ActionlessForm" Assembly="ActionlessForm" %>
將頁面中的<form runat="server"></form>替換成:<skm:form id="form1" runat="server"></skm:form>
注:以這種方式處理回發將會在設計器中查看的時候爲錯誤提示Form不可用
所以採用以下方法處理:
在微軟的URLRewriter類庫中添加以下類之後編譯
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
/// <summary>
/// FormRewriter 的摘要說明
/// </summary>
namespace URLRewriter.Form
{
public class FormRewriterControlAdapter : System.Web.UI.Adapters.ControlAdapter
{
public FormRewriterControlAdapter()
{
}
protected override void Render(HtmlTextWriter writer)
{
base.Render(new RewriteFormHtmlTextWriter(writer));
}
}
public class RewriteFormHtmlTextWriter : HtmlTextWriter
{
public RewriteFormHtmlTextWriter(HtmlTextWriter writer)
: base(writer)
{
base.InnerWriter = writer.InnerWriter;
}
public RewriteFormHtmlTextWriter(System.IO.TextWriter writer)
: base(writer)
{
base.InnerWriter = writer;
}
public override void WriteAttribute(string name, string value, bool fEncode)
{
//If the attribute we are writing is the "action" 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 == "action")
{
HttpContext context = HttpContext.Current;
if (context.Items["ActionAlreadyWritten"] == 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 <form>'s action attribute to prevent
//us from rewriting a sub-control under the <form> control
context.Items["ActionAlreadyWritten"] = true;
}
}
base.WriteAttribute(name, value, fEncode);
}
}
}
在App_Browsers文件夾下創建Form.browser
<browsers>
<browser refID="Default">
<controlAdapters>
<adapter controlType="System.Web.UI.HtmlControls.HtmlForm"
adapterType="URLRewriter.Form.FormRewriterControlAdapter" />
</controlAdapters>
</browser>
</browsers>
這樣就不需要引用ActionlessForm.dll也不需要改變Form了,只要引用URLRewriter.dll就可以了
三、在處理重寫成html的時候本來網站中的html頁面將會不能使用
使用以上方式將不存在找個問題
如果還不行可以在<compilation debug="true">節點下添加
<buildProviders>
<add extension=".html" type="System.Web.Compilation.PageBuildProvider" />
</buildProviders>
在<httpHandlers>節點下添加(如果之前使用的是http處理程序執行重寫的,請寫在前面)
<add path="*.html" verb="*" type="System.Web.UI.PageHandlerFactory" validate="true"/>
以下爲IIS7.0配置方式
IIS7不再使用URLRewriter.dll組件了,請使用IIS7的URLWRITER,下載rewrite_2.0_rtw並進行安裝,配置