使用rewrite在iis上設定規則來實現偽靜態,這樣在vs中開發的時候將無法調試,必須要是用iis發布才不會造成找不到檔案的錯誤,當然,肯定還有其他的方法來實現偽靜態,不用再iis中設定任何的東西,只需要一個。net的組件和設定一下webconfig檔案就ok了,來看一下,這東西,我用的次數很少,每次用都怕忘記,所以記錄下來,下次忘記的話也不用Google啦
這回的準備工作,你必須去網上download一個URLRewriter.dll組件了,網上貌似有這個類庫的原始碼,當然裡面也有編譯好的dll檔案,只需要把dll檔案coty到自己網站的Bin下面,然後在網站上右鍵,添加引用,瀏覽選擇這個dll檔案即可。
下面就需要在webconfig裡面配置一番了
1、在Web.Config的<system.web>和</system.web>添加以下節點:
<httpModules>
<add type="URLRewriter.ModuleRewriter, URLRewriter" name="ModuleRewriter" />
</httpModules>
2、在Web.Config的<configuration>和</configuration>添加以下節點:
<configSections> <section name="RewriterConfig" type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter" /> </configSections> <RewriterConfig> <Rules> <RewriterRule> <LookFor>~/Default/.html</LookFor> <SendTo>~/Default.aspx</SendTo> </RewriterRule> <RewriterRule> <LookFor>~/Detial_([0-9]*)/.html</LookFor> <SendTo>~/Detial.aspx?Id=$1</SendTo> </RewriterRule> <RewriterRule> <LookFor>~/List_p([0-9]*)/.html</LookFor> <SendTo>~/List.aspx?page=$1</SendTo> </RewriterRule> <RewriterRule> <LookFor>~/Double_t([0-9]*)_p([0-9]*)/.html</LookFor> <SendTo><!--[CDATA[~/Double.aspx?type=$1&page=$2]]--></SendTo> </RewriterRule> <RewriterRule> <LookFor>~/About_(.*)/.html</LookFor> <SendTo>~/About.aspx?title=$1</SendTo> </RewriterRule> </Rules> </RewriterConfig>
設定完以上兩個步驟,就基本可以運行了,其中<RewriterRule/>標籤裡面就是重寫的一個模組,<LookFor/>裡面的是重寫後的地址,而<SendTo/>則是原地址。大家應該注意到了我寫了2組<RewriterRule/>,其中第一組的是給單參數的地址用的,而另一組是給多參數動態檔案用的。
相信大家如果用過在iis裡面配置偽靜態規則的話,那在webconfig裡面配置規則自然不在話下,如果不會的話,就去找一些Regex的書看一下,不需要很精通就行
當然此方法有一些注意事項:
1.不能使用Windows身分識別驗證使用者權限. 應使用Form驗證,在web.config配置為:<authentication mode="Forms" />
2.使用Request.ServerVariables["script_name"]獲得的路徑仍然是:ShowPlay.asp?vid=1
3.被重寫的地址如果回傳,重寫將失效 顯示的地址將是ShowPlay.asp?vid=1
4. 尾碼名必須為.aspx.如果是其他自訂尾碼名,如.net 請在iis將.net映射到aspnet_isapi.dll.
五個頁面
About.aspx
<form id="form1" runat="server"> <div> 這是Aboutl頁,url是About.aspx還是About.html?<br/> <br/> <br/> <a href="Default.html" mce_href="Default.html">首頁</a> <br /> <br /> <a href ="List_p1.html">List頁傳page參數</a> <br /> <br /> <a href ="Detial_1.html">Detial頁傳值Id參數</a> <br /> <br /> <a href ="About_jianjie.html">About頁傳值title參數</a> <br /> <br /> <a href ="Double_t1_p1.html">Double頁傳值type和page參數</a> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> </div> </form>
cs代碼:
//About頁面public partial class About : System.Web.UI.Page{ protected void Page_Load(object sender, EventArgs e) { string title = Request["title"].ToString(); TextBox1.Text = "name為" + title; }}//Detial頁面public partial class Detial : System.Web.UI.Page{ protected void Page_Load(object sender, EventArgs e) { string Id = Request["Id"].ToString(); TextBox1.Text = "Id號為" + Id; }}//Double.aspx頁面public partial class Double : System.Web.UI.Page{ protected void Page_Load(object sender, EventArgs e) { string type = Request["type"].ToString(); string page = Request["page"].ToString(); TextBox1.Text = "type為:" + type + "page為" + page; }}//List.aspx頁面public partial class List : System.Web.UI.Page{ protected void Page_Load(object sender, EventArgs e) { string page = Request["page"].ToString(); TextBox1.Text = "頁數為" + page; }}