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

本文基本ASP.NET技术,重写URL不需要IIS设置。
本教程目的,利用UrlRewriter重写组件来重写URL地址,来达到SEO,隐藏URL参数。
首先下载UrlRewriter
下载地址:
http://down.chinaz.com/soft/23286.htm
 
下面以一个简单的实例来做应用:
第一、把下载好的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来静态访问了。

文章来自学IT网:http://www.xueit.com/html/2009-02/21_494_00.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值