官方QQ群:127876820【C#编程技术-全国站--未满人】

ASP.NET重写URL地址,主要利用UrlRewriter

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

第一、把下载好的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>

   原访问效果图:

      

   重写效果图:

      

 

    第二种效果实验:

    正常访问: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>

   效果图如下:

      

 

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


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

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

 

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

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

 

 

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

 

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

 

posted @ 2010-09-22 19:08  碧海蓝天_C#  阅读(387)  评论(0编辑  收藏  举报
官方QQ群:127876820【C#编程技术-全国站--未满人】