天天看點

C#之僞靜态實作

所謂的僞靜态就是把通路的網址僞裝成其他的自己指定的網址的方式。

步驟如下:

1、首先下載下傳一個URLRewriter.dll(這玩意随便百度一下都可以查到的,檔案大小20k左右)

然後在你所要使用的項目中引用URLRewriter.dll

2、配置web.config

首先申明一個便簽,該标簽用于定義規則

<!--申明标簽-->

    <configSections>

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

    </configSections>

使用定義的标簽來定義映射規則

    <!--使用申明的标簽-->

    <RewriterConfig>

        <!--定義規則-->

        <Rules>

            <RewriterRule>

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

                <SendTo>~/Default.aspx</SendTo>

            </RewriterRule>

            <RewriterRule>

                <LookFor>~/index/([0-9]*)/([0-9]*).html</LookFor><!--[0-9]*是指任意數字-->

                <SendTo>~/Default.aspx?p1=$1&amp;p2=$2</SendTo>

            </RewriterRule>

        </Rules>

    </RewriterConfig>

然後在<system.web>中指定映射,添加一個

<!--為頁面映射,沒添加是沒有效果的-->

        <httpHandlers>

            <!--使用URLRewriter.dll-->

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

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

        </httpHandlers>

3、通路如下:頁面通路的url如下:

http://localhost:14923/Default.aspx

相當于

http://localhost:14923/index.html

http://localhost:14923/Default.aspx?p1=4&p2=5

相當于

http://localhost:14923/index/4/5.html

C#之僞靜态實作