当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > asp.net URL重写(URLRewriter) 简化版

ASP.NET
在图片上加入图片版权信息
Peer-to-Peer (P2P) communication across middleboxes(翻译4)
今天完成了.net compact framework 加 web service的演练.
Cordbg, Dumpbin, Ildasm, 的一些教程。
asp和asp.net的session共用
VB连接SQL数据库的模块
消除图片在ie中缓存而无法更新的问题
说说使用static和const关键字
怎样解决thephile中的数据库由于排序造成的问题:对 text 数据类型不支持代码页转...
.net分布式事务例子
Internet Explorer 编程简述(二)
使用SqlParameter参数返回值时遇到的问题
vb可不可以实现虚拟中断
C#下Socket对象的BeginReceive方法,执行后竟然不调用AsyncCallback里的回调函数
坚持学asp.net:(十一)
[C#][正则表达式]寻找匹配的Groups的几种方法
面向服务的体系结构概述
Windows Form 和 UserControl
VB中類模塊實現與C++中類實現的比較(1)
下载Oracle数据库中的Blob二进制文件,实例!

ASP.NET 中的 asp.net URL重写(URLRewriter) 简化版


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-08-14   浏览: 98 ::
收藏到网摘: n/a

  在 asp.net 里实现 URL重写(URLRewriter)的一个最简单的方法。

  参考了 (作者 Scott Mitchell 翻译:Janssen )的大作,虽然没有完全看明白,但是也照猫画虎地做了一个,颇有“成就”感。写出来分享一下。

  原作里讲了很多的原理,这里就不说了(其实我也不懂)。这里就写操作过程吧。目的是实现一个最简单的能实现 URL重写 的程序。

  1、需要设置一下IIS里的站点属性。

  2、修改web.config的内容。

   <system.web>
   <httpHandlers>
   <add verb="*" path="*.zhtml" type="ZDIL.URLRewriter.RewriterFactoryHandler, ZDILURLRewriter" />
  </httpHandlers>
  </system.web>

  其中*.zhtml 就是地址栏里面写的网页的扩展名,就是给用户看的,这个可以随意改(但是要符合扩展名的规则!)。当然要和第一步里面的设置相一致才行。

  3、写一个类。

  using System;
  using System.IO;
  using System.Web;
  using System.Web.UI;

  namespace ZDIL.URLRewriter
  {
   /**//// <summary>
   /// URL重写
  /// </summary>
   public class RewriterFactoryHandler : IHttpHandlerFactory
    {
    /**//// <summary>
   /// GetHandler is executed by the ASP.NET pipeline after the associated HttpModules have run.  The job of
        /// GetHandler is to return an instance of an HttpHandler that can process the page.
        /// </summary>
        /// <param name="context">The HttpContext for this request.</param>
        /// <param name="requestType">The HTTP data transfer method (<b>GET</b> or <b>POST</b>)</param>
        /// <param name="url">The RawUrl of the requested resource.</param>
        /// <param name="pathTranslated">The physical path to the requested resource.</param>
        /// <returns>An instance that implements IHttpHandler; specifically, an HttpHandler instance returned
        /// by the <b>PageParser</b> class, which is the same class that the default ASP.NET PageHandlerFactory delegates
        /// to.</returns>
        public virtual IHttpHandler GetHandler(HttpContext context, string requestType, string url, string pathTranslated)
        {
            string sendToUrl = url;                         //地址栏里面的地址
            string filePath = pathTranslated;

            string sendToURLString = "/web/index.aspx";  //真正要访问的页面
            string queryString = "";                     //参数。比如 ?id=123

            filePath = context.Server.MapPath(sendToURLString); //物理地址

            //这句最重要了。转向了。
            context.RewritePath(sendToURLString, String.Empty, queryString);
           
            //这个还没有弄明白 :)
            return PageParser.GetCompiledPageInstance(url, filePath, context);
        }

        public virtual void ReleaseHandler(IHttpHandler handler)
        { //这个也不懂了
        }
    }
}
 

  这个类呢,要写在一个单独的项目里面,然后编译成 ZDILURLRewriter.DLL文件。(注意文件名,写错了就不能正常运行了)。

  4、完成了。

  打开IE ,在地址栏里输入 http://.../1.zhtml。

  浏览者看到是一个静态页的地址,但是实际上访问的却是 /web/index.aspx 这个动态网页。

  怎么样简单吧。

  当然了,这个是最简单的,简单到了“不能用”的地步了。因为他会把所有的 *.zhtml 的访问都“重写”到 /web/index.aspx 。

  至于把什么样的网页重写到哪个网页,这里就不介绍了(这里只讲方法,不讲实现的细节)。

  方法很多了,原作是通过正则来匹配的,我是通过 string sendToUrl = url; 来判断的。

  其他的就看你们的需要了。

  http://blog.csdn.net/shixin1198/archive/2006/10/16/1336846.aspx