zoukankan      html  css  js  c++  java
  • Urlrewrite方法集

    Discuz!NT HttpModule重定向方法

     using System;
    using System.Diagnostics;
    using System.Threading;
    using System.Web;
    using System.Xml;
    using System.Text.RegularExpressions;
    using System.IO;

    namespace pureland.UrlRewrite
    {
     /// <summary>
     /// HttpModule类
     /// </summary>
     public class HttpModule:System.Web.IHttpModule
     {
      /// <summary>
      /// 实现接口的Init方法
      /// </summary>
      /// <param name="context"></param>
      public void Init(HttpApplication context)
      {
       context.BeginRequest+=new EventHandler(ReUrl_BeginRequest);
      }

      public void Application_OnError(Object sender , EventArgs e)
      {
      }

      /// <summary>
      /// 实现接口的Dispose方法
      /// </summary>
      public void Dispose()
      {
      }

      /// <summary>
      /// 重写URL
      /// </summary>
      /// <param name="sender">事件的源</param>
      /// <param name="e">包含事件数据的</param>
      private void ReUrl_BeginRequest(object sender,EventArgs e)
      {
       HttpContext context = ((HttpApplication)sender).Context;
       string requestPath = context.Request.Path.ToLower();
       context.RewritePath("Test.aspx");
      }
     }
    }

    Web.config 中添加

        <httpModules>
            <add type="pureland.UrlRewrite.HttpModule, pureland.UrlRewrite" name="HttpModule" />
        </httpModules>

    、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、

    、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、

    在网上看到,很多朋友在asp.net中做urlrewrite,用的是HttpHandle+Server.Transfer的方法。其实这种方法是错误的。第一,HttpHandle是实现不了urlrewrite的;第二Server.Transfer是标准的重定向,根本不是urlrewrite。

    其实,实现urlrewrite不用自己HttpHandle,也不用自己实现HttpModule,用几行代码就可以轻松实现。

    我这里介绍的是在虚拟主机上,虚拟主机不同于自己的服务器,你是没有权限去修改IIS,也没有权限去安装iis rewrite之类的IIS插件。但是我们仍然可以轻松完成需要的功能。

    具体做法如下:打开global.asax.cs,定位到protected void Application_BeginRequest(Object sender, EventArgs e)。从方法名我想也能猜到它是做什么的。输入如下代码:

     

    protected void Application_BeginRequest(Object sender, EventArgs e)
                {
                string oldUrl = HttpContext.Current.Request.RawUrl ;
                string pattern = @"^(.+)default/(\d+)\.aspx(\?.*)*___FCKpd___0quot;;
                string replace = "$1default.aspx?id=$2";
                if(Regex.IsMatch(oldUrl, pattern, RegexOptions.IgnoreCase | RegexOptions.Compiled))
                {
                string newUrl = Regex.Replace(oldUrl, pattern, replace, RegexOptions.Compiled |
                RegexOptions.IgnoreCase);
                this.Context.RewritePath(newUrl);
                }
                }


    有了上边这段代码,我访问一个类似:.../default/123.aspx 的网址,当然这个网址在我的电脑上不存在,它就会被定向到:.../default.aspx?id=123。

    当然,利用功能强大的正则表达式,你可以任意按照自己的需要来重写url,这一切都是在服务器端默默的进行,在客户端是不会有任何察觉的。由于是在虚拟主机上,我们只能重定向.aspx文件,如果是自己的服务器,只要把后缀名在IIS中注册一下,就可以实现任意后缀名的处理。比如你可以注册一个*.myweb这样的类型,这样别人访问default/456.myweb时,你可以把它重定向到default.aspx?id=456。总之一句话,只要你能想到.net就可以帮你实现,并且这一切不需要多少的代码。


     

  • 相关阅读:
    PAT1124:Raffle for Weibo Followers
    Pat1071: Speech Patterns
    PAT1032: Sharing (25)
    Pat1128:N Queens Puzzle
    C++相关:C++的IO库
    Pat1108: Finding Average
    PAT1070:Mooncake
    乐港游戏校招面试总结
    并发编程005 --- future &&futureTask
    并发编程004 --- 线程池的使用
  • 原文地址:https://www.cnblogs.com/aaa6818162/p/1452156.html
Copyright © 2011-2022 走看看