asp.net URL重写(URLRewriter) 简化版

作者:凯旋网络来源:凯旋网络

  在 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、写一个类。

Www~Chinaz~com

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

Chinaz

  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>

Chinaz@com


        /// <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;                         //地址栏里
  • 广告推荐