C#之伪静态实现

来源:互联网 发布:文本编辑 mac 编辑:程序博客网 时间:2024/05/01 20:29

所谓的伪静态就是把访问的网址伪装成其他的自己指定的网址的方式。

步骤如下:

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


0 0
原创粉丝点击