天天看點

.net 實作 URL重寫,僞靜态(轉)

 下載下傳完畢後,導入工程,我這裡沒有對該工程做任何修改,保留了原來的重寫方式,然後直接在VS2005裡面生成.dll檔案就可以了。

二,使用該dll檔案:

添加引用,搞定。

三,頁面方面的設計,這裡不在贅述了,我會放一個下載下傳包,有興趣的朋友下載下傳來看看吧,代碼寫的比較亂。

四,web.config的配置

這部是非常關鍵的,也是靜态化能否成功的關鍵。

view plaincopy to clipboardprint?

<?xml version="1.0"?>  

<configuration>  

  <configSections>  

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

  </configSections>  

  <RewriterConfig>  

        <Rules>  

            <RewriterRule>  

                <LookFor>~/web/new/type/(.[0-9]*)\.html</LookFor>  

        <SendTo>~/web/new.aspx?id=$1</SendTo>  

            </RewriterRule>  

      <RewriterRule>  

        <LookFor>~/web/index.html</LookFor>  

        <SendTo>~/web/index.aspx</SendTo>  

      </RewriterRule>  

        </Rules>  

    </RewriterConfig>  

    <system.web>  

    <httpHandlers>  

      <add verb="*" path="*.aspx" type="URLRewriter.RewriterFactoryHandler, URLRewriter" />  

      <add verb="*" path="*.html" type="URLRewriter.RewriterFactoryHandler, URLRewriter" />  

    </httpHandlers>  

        <compilation debug="true"/></system.web>  

</configuration>  

<?xml version="1.0"?>

<configuration>

  <configSections>

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

  </configSections>

  <RewriterConfig>

        <Rules>

            <RewriterRule>

                <LookFor>~/web/new/type/(.[0-9]*)\.html</LookFor>

        <SendTo>~/web/new.aspx?id=$1</SendTo>

            </RewriterRule>

      <RewriterRule>

        <LookFor>~/web/index.html</LookFor>

        <SendTo>~/web/index.aspx</SendTo>

      </RewriterRule>

        </Rules>

    </RewriterConfig>

    <system.web>

    <httpHandlers>

      <add verb="*" path="*.aspx" type="URLRewriter.RewriterFactoryHandler, URLRewriter" />

      <add verb="*" path="*.html" type="URLRewriter.RewriterFactoryHandler, URLRewriter" />

    </httpHandlers>

        <compilation debug="true"/></system.web>

</configuration>

這裡簡單介紹一下:

<RewriterConfig>  

   <Rules>  

   <RewriterRule>  

      <LookFor>要查找的模式</LookFor>  

      <SendTo>要用來替換模式的字元串</SendTo>  

   </RewriterRule>  

   </Rules>  

</RewriterConfig>  

<RewriterConfig>

   <Rules>

   <RewriterRule>

      <LookFor>要查找的模式</LookFor>

      <SendTo>要用來替換模式的字元串</SendTo>

   </RewriterRule>

   </Rules>

</RewriterConfig>

httpHandlers的設定主要是配合IIS将請求重新定義處理,這裡也比較關鍵,如果不存在合理的httpHandlers,那麼,通路肯定會失敗的。

關于正規表達式,可以到百度裡搜尋:"常用正規表達式",會有很多。

五.配置IIS解析.html檔案

右鍵點我的電腦-->管理-->展開'服務和應用程式'-->internet資訊服務-->找到你共享的目錄-->右鍵點選屬性 -->點選'配置'-->映射下面 -->找到.aspx的可執行檔案路徑 複制路徑-->粘貼路徑-->擴充名為".html"-->然後把檢查檔案是否存在的勾去掉這樣就可以了,如果遇到“确定”按鈕失效,可以用鍵盤事件編輯路徑即可解決。

本文轉自94cool部落格園部落格,原文連結:http://www.cnblogs.com/94cool/articles/1499341.html,如需轉載請自行聯系原作者