首页 | 源码下载 | 网站模板 | 网页特效 | 广告代码 | 网页素材 | 字体下载 | 书库 | 站长工具
会员投稿 投稿指南 RSS订阅
当前位置:主页>网络编程>asp.net>资讯:ASP.NET重写URL地址,主要利用UrlRewriter组件

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

www.jz123.cn  2009-02-15   来源:   中国建站会员投稿    网客心飞    我要投递新闻

  本文版权归学IT网(www.xueit.com)所有,任何单位与个人转载必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

  本文基本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网会出更多原创作品。


上一篇:在VB.NET中利用Split和Replace函数计算字数 下一篇:Asp.net通过MySql.Data.dll操作mysql数据库

评论总数:3 [ 查看全部 ] 网友评论


关于我们隐私版权广告服务友情链接联系我们网站地图