利用 HttpModule 重写 URL

    void context_BeginRequest(object sender, EventArgs e)

    {

        HttpApplication application = (HttpApplication)sender;



        HttpContext context = application.Context;

        HttpResponse response = application.Response;



        string path = context.Request.Path;



        string file = System.IO.Path.GetFileName(path);



        Regex regex = new Regex(@"(\w+)_(\w+)(\d).html", RegexOptions.Compiled);

        Regex regex2 = new Regex(@"(\w+).html", RegexOptions.Compiled);



        Match match = regex.Match(file);

        Match match2 = regex2.Match(file);





        if (match2.Success)

        {

            string url = match2.Groups[1].Value;

            context.RewritePath(url + ".aspx");

        }

        if (match.Success)

        {

            string url = match.Groups[1].Value;

            string parm = match.Groups[2].Value;

            string parmValue=match.Groups[3].Value;



            string reWritePath = url + ".aspx?" + parm + "=" + parmValue;



            context.RewritePath(reWritePath);

        }



    }



    #region IHttpModule 成员



    public void Dispose()

    {

        throw new NotImplementedException();

    }



    public void Init(HttpApplication context)

    {

        context.BeginRequest += new EventHandler(context_BeginRequest);

    }



    #endregion

  1. <httpModules>  
  2.  <add name="UrlReWriter" type="UrlReWriter"/>  
  3.  </httpModules>  

如果要实现全站伪静态需要在IIS配置中添加.html映射

源:http://blog.csdn.net/zhoufoxcn/archive/2009/07/14/4346356.aspx

你可能感兴趣的:(Module)