.net 實現(xiàn) URL重寫,偽靜態(tài)

字號:


    一,獲得Mircosoft URLRewriter.dll:
    獲得Mircosoft URLRewriter.dll可以到
    下載完畢后,導(dǎo)入工程,我這里沒有對該工程做任何修改,保留了原來的重寫方式,然后直接在VS2005里面生成.dll文件就可以了。
    二,使用該dll文件:
    添加引用,搞定。
    三,頁面方面的設(shè)計,這里不在贅述了,我會放一個下載包,有興趣的朋友下載來看看吧,代碼寫的比較亂。
    四,web.config的配置
    這部是非常關(guān)鍵的,也是靜態(tài)化能否成功的關(guān)鍵。
    <?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的設(shè)置主要是配合IIS將請求重新定義處理,這里也比較關(guān)鍵,如果不存在合理的httpHandlers,那么,訪問肯定會失敗的。
    關(guān)于正則表達式,可以到百度里搜索:"常用正則表達式",會有很多。
    五.配置IIS解析.html文件
    右鍵點我的電腦-->管理-->展開'服務(wù)和應(yīng)用程序'-->internet信息服務(wù)-->找到你共享的目錄-->右鍵點擊屬性 -->點擊'配置'-->映射下面 -->找到.aspx的可執(zhí)行文件路徑 復(fù)制路徑-->粘貼路徑-->擴展名為".html"-->然后把檢查文件是否存在的勾去掉這樣就可以了,如果遇到“確定”按鈕失效,可以用鍵盤事件編輯路徑即可解決。