關於.net 的UrlRewrite


  其實一直有這個疑問,為什么人家.net 寫的網站為什么后綴能變成.html來顯示?今天終於明白了,答案:UrlRewrite

  步驟:添加URLRewriter.dll ,網上自己下載,還有一個ActionlessForm.dll,是處理post數據的。然后在web.config中配置頁面信息,具體如下:

 1、往<configuration>節點中添加

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

2、在<configuration>下的<system.web>節點中添加

<httpModules>
      <add type="URLRewriter.ModuleRewriter, URLRewriter" name="ModuleRewriter"/>
</httpModules>

3、開始在<configuration>下寫Rewriter配置信息,url信息用正則表達式表示

<RewriterConfig>
    <Rules>
      <!-- 規則,可以使用正則表達式 -->
      <RewriterRule>
        <LookFor>~/index.html</LookFor>
        <SendTo>~/Index.aspx</SendTo>
      </RewriterRule>
      <RewriterRule>
        <LookFor>~/News.html</LookFor>
        <SendTo>~/News.aspx</SendTo>
      </RewriterRule>
      <RewriterRule>
        <LookFor>~/NewsShow_(\d{1,4})\.html</LookFor>
        <SendTo>~/NewsShow.aspx?id=$1</SendTo>  

      </RewriterRule>

  <RewriterRule>
      <LookFor>~/wjProduct-(\d{1,6})-(\d{1,6})\.html</LookFor>
      <SendTo>~/wjProduct.aspx?page=$1&amp;Menu_ID=$2</SendTo>  //第一個參數用$1;第二個參數用$2...  參數中間加&amp

    </RewriterRule>
  。。。

  。。。     

    </Rules>
  </RewriterConfig>

4、接下來就是每個url的寫法了,比如你在index.aspx頁面的主頁為index.html ,但是真正發送請求出去的是index.aspx,其他也一樣道理。再在項目的屬性中設置啟動為index.html;這樣一運行就執行了index.html。

 

注意:這樣重寫可能會致使有些路徑錯亂,如圖片路徑。畢竟你是在項目都弄完后才來rewrite的,所以要小心。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM