URl重写实例

来源:互联网 发布:微店 淘宝网页制作 编辑:程序博客网 时间:2024/06/06 18:14

下面以一个简单的实例来做应用:

第一、把下载好的Intelligencia.UrlRewriter.dll文件放到程序目录bin

     再到vs.net项目中添加Intelligencia.UrlRewriter.dll文件引用

第二、新建一个index.aspx文件

     index.aspx.cs内容如下:

        protected void Page_Load(object sender, EventArgs e)

        {

            Response.Write(Request["pid"]);

        }

     也就是说输出参数pid的内容

第三、配置Web.config文件

     内容如下:

  <?xml version="1.0" encoding="utf-8"?>

  <configuration>

    <!--设置地址重写组件-->

    <configSections>

   <section name="rewriter" requirePermission="false" type="Intelligencia.UrlRewriter.Configuration.RewriterConfigurationSectionHandler, Intelligencia.UrlRewriter"/>

    </configSections>

    <!--地址重写规则文件-->

    <rewriter file="~/App_Data/rewrite.xml"/>

    <appSettings/>

    <connectionStrings/>

   <system.web>

    <compilation debug="false">

    </compilation>

    <authentication mode="Windows" />

     <!--httpModules-->

     <httpModules>

    <add type="Intelligencia.UrlRewriter.RewriterHttpModule,Intelligencia.UrlRewriter" name="UrlRewriter" />

     </httpModules>

   </system.web>

   </configuration>

第四、开始实例应用

    第一种效果实验:

    正常访问:http://localhost:4412/index.aspx?pid=hao

    重写URL为:http://localhost:4412/index_hao.aspx

    重写规则文件rewrite.xml内容如下:

   <?xml version="1.0" encoding="utf-8" ?>

   <rewriter>

      <!-- ([a-zA-Z]+) 只允许英文 -->

      <rewrite url="~/index_([a-zA-Z]+).aspx$" to="~/index.aspx?pid=$1" processing="stop" />

   </rewriter>

   原访问效果图:

       url重写地址 - 清水之滨 - 温州大学生兼职网

   重写效果图:

       url重写地址 - 清水之滨 - 温州大学生兼职网

 

    第二种效果实验:

    正常访问:http://localhost:4412/index.aspx?pid=hao

    重写URL为:http://localhost:4412/index/任意字符.aspx

    重写规则文件rewrite.xml内容如下:

   <?xml version="1.0" encoding="utf-8" ?>

   <rewriter>

      <!-- ([a-zA-Z]+) 只允许英文 -->

      <rewrite url="~/index_([a-zA-Z]+).aspx$" to="~/index.aspx?pid=$1" processing="stop" />

      <!-- (.*)任意字符 -->

      <rewrite url="~/index/(.*)/.aspx$" to="~/index.aspx?pid=$1" />

   </rewriter>

   效果图如下:

       url重写地址 - 清水之滨 - 温州大学生兼职网

 

实验成功,是不是很简单呢!!欢迎多多交流,学it网会出更多原创作品。

注意: 传递多个参数使用&amp;

如:<rewrite url="~/index/(.*)/(.*)/.aspx$" to="~/index.aspx?pid=$1&amp;act=$2" />

 

补充讲解实现.aspx转为.html的方法:

其实很简单,只要一步操作即可,下面以ASP.NET2.0为例如图设置:

url重写地址 - 清水之滨 - 温州大学生兼职网

 

可执行文件路径为:C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/aspnet_isapi.dll

 

这样就可以把上面的规则为.aspx改为.html来静态访问了。

原创粉丝点击