利用Mircosoft URLRewriter.dll實現頁面偽靜態

來源:互聯網
上載者:User

一,獲得Mircosoft URLRewriter.dll:

獲得Mircosoft URLRewriter.dll可以到http://www.microsoft.com/china/msdn/library/webservices/asp.net/URLRewriting.mspx?mfr=true

這裡,選擇[下載本文的原始碼],下載完畢後,匯入工程,我這裡沒有對該工程做任何修改,保留了原來的重寫方式,然後直接在VS2005裡面產生.dll檔案就可以了。

二,使用該dll檔案:

添加引用,搞定。

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

四,web.config的配置

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

<?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>

   <RewriterRule>

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

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

   </RewriterRule>

   </Rules>

</RewriterConfig>

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

關於Regex,可以到百度裡搜尋:"常用Regex",會有很多。

五.配置IIS解析.html檔案

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

本文執行個體下載:http://files.cnblogs.com/zhangyi85/StaticWeb.rar

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.