RSS订阅优然探索
你的位置:首页 » 技术笔记 » 正文

C# HttpModule文章(一)

选择字号: 超大 标准 发布时间:2008-9-10 21:8:42 | 作者:admin | 0个评论 | 人浏览

/*
 * HttpModule Version: 1.0
 * Created on 2008-09-10
 *
 * Web: http://www.0451sky.com
 * Copyright (C) 2008 - 2010 CareF Technology Inc., All Rights Reserved.
 * 版权所有 (C) 时间 公司名称 Inc., All Rights Reserved.
 */


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


namespace CareF.HttpRewrite
{
 /// <summary>
 /// HttpModule 的摘要说明。
 /// </summary>
 public class HttpModule : System.Web.IHttpModule
 {
  //public readonly static Mutex m=new Mutex();

  /// <summary>
  /// 实现接口的Init方法
  /// </summary>
  /// <param name="context"></param>
  public void Init(HttpApplication context)
  {
   //OnlineUserFactory.ResetOnlineList();
   context.BeginRequest += new EventHandler(ReUrl_BeginRequest);
  }

  public void Application_OnError(Object sender , EventArgs e)
  {
   HttpApplication application = (HttpApplication)sender;
   HttpContext context = application.Context;
   //if (context.Server.GetLastError().GetBaseException() is MyException)
  {
   //MyException ex = (MyException) context.Server.GetLastError().GetBaseException();
   context.htm = htm&("<html><body style=\"font-size:14px;\">");
   context.htm = htm&("CareF Error:<br />");
   context.htm = htm&("<textarea name=\"errormessage\" style=\"width:80%; height:200px; word-break:break-all\">");
   context.htm = htm&(System.Web.HttpUtility.HtmlEncode(context.Server.GetLastError().ToString()));
   context.htm = htm&("</textarea>");
   context.htm = htm&("</body></html>");
   context.Response.End();
  }

  }

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

  /// <summary>
  /// 重写Url
  /// </summary>
  /// <param name="sender">事件的源</param>
  /// <param name="e">包含事件数据的 EventArgs</param>
  private void ReUrl_BeginRequest(object sender, EventArgs e)
  {
   HttpContext context = ((HttpApplication)sender).Context;
   //string requestPath = context.Request.Path.ToLower();
   string path = context.Request.Path.ToLower();
   string forumPath = context.Request.Path.ToLower();
   foreach(SiteUrls.URLRewrite url in SiteUrls.GetSiteUrls().Urls)
   {
    if (Regex.IsMatch(path, url.Pattern, RegexOptions.Compiled|RegexOptions.IgnoreCase))
    {
     string newUrl = Regex.Replace(path, url.Pattern, url.QueryString, RegexOptions.Compiled|RegexOptions.IgnoreCase);
     //context.RewritePath(forumPath + "archiver" + url.Page, string.Empty, newUrl);
     context.RewritePath(url.Page, string.Empty, newUrl);
     //context.RewritePath(文件路径,文件夹路径,参数);
     return;
    }
   }
   return;

  }

 }

 

 
 /// <summary>
 /// SiteUrls get HttpModuleurls.config files urls's rewrite
 /// </summary>
 public class SiteUrls
 {
  #region 内部属性和方法
  
  #region Properties
   public System.Collections.ArrayList Urls
   {
    get
    {
     return _Urls;
    }
    set
    {
     _Urls = value;
    }
   }

   public System.Collections.Specialized.NameValueCollection Paths
   {
    get
    {
     return _Paths;
    }
    set
    {
     _Paths = value;
    }
   }
  #endregion
  #region Fields
   private static object lockHelper = new object();
   private static volatile SiteUrls instance = null;
   string SiteUrlsFile = HttpContext.Current.Server.MapPath("/HttpModuleurls.config");
   private System.Collections.ArrayList _Urls;
   private System.Collections.Specialized.NameValueCollection _Paths;
  #endregion
  #region Methods
  /// <summary>
  /// GetSiteUrls() get .config files notes
  /// </summary>
  /// <returns></returns>
  public static SiteUrls GetSiteUrls()
  {
   if (instance == null)
   {
    lock (lockHelper)
    {
     if (instance == null)
     {
      instance = new SiteUrls();
     }
    }
   }
   return instance;

  }

  public static void SetInstance(SiteUrls anInstance)
  {
   if (anInstance != null)
    instance = anInstance;
  }

  public static void SetInstance()
  {
   SetInstance(new SiteUrls());
  }

  /// <summary>
  /// 输出URL示例
  /// </summary>
  /// <param name="id"></param>
  /// <returns></returns>
  public string Show(int id)
  {
   return string.Format(Paths["Show"], id);
  }
  #endregion
  
  /// <summary>
  /// SiteUrls structure function
  /// </summary>
  private SiteUrls()
  {
   Urls = new System.Collections.ArrayList();
   Paths = new System.Collections.Specialized.NameValueCollection();
   XmlDocument xml = new XmlDocument();
   xml.Load(SiteUrlsFile);
   XmlNode root = xml.SelectSingleNode("urls");
   foreach(XmlNode n in root.ChildNodes)
   {
    if (n.NodeType != XmlNodeType.Comment && n.Name.ToLower() == "rewrite")
    {
     XmlAttribute name = n.Attributes["name"];
     XmlAttribute path = n.Attributes["path"];
     XmlAttribute page = n.Attributes["page"];
     XmlAttribute querystring = n.Attributes["querystring"];
     XmlAttribute pattern = n.Attributes["pattern"];

     if (name != null && path != null && page != null && querystring != null && pattern != null)
     {
      Paths.Add(name.Value, path.Value);
      Urls.Add(new URLRewrite(name.Value, pattern.Value, page.Value.Replace("^", "&"), querystring.Value.Replace("^", "&")));
     }
    }
   }
  }
  #endregion
  /// <summary>
  /// URLRewrite for save url param
  /// </summary>
  public class URLRewrite
  {
   #region Properties
    public string Name
    {
     get
     {
      return _Name;
     }
     set
     {
      _Name = value;
     }
    }

    public string Pattern
    {
     get
     {
      return _Pattern;
     }
     set
     {
      _Pattern = value;
     }
    }
    public string Page
    {
     get
     {
      return _Page;
     }
     set
     {
      _Page = value;
     }
    }

   /// <summary>
   /// get QueryString info for RegEx
   /// </summary>
   public string QueryString
   {
    get
    {
     return _QueryString;
    }
    set
    {
     _QueryString = value;
    }
   }
   #endregion

   #region Fields
    private string _Name;
    private string _Pattern;
    private string _Page;
    private string _QueryString;
   #endregion

   #region Methods

   #endregion
   
   #region 构造函数
   /// <summary>
   /// URLRewrite Save Url to Class
   /// </summary>
   /// <param name="name"></param>
   /// <param name="pattern"></param>
   /// <param name="page"></param>
   /// <param name="querystring"></param>
   public URLRewrite(string name, string pattern, string page, string querystring)
   {
    _Name = name;
    _Pattern = pattern;
    _Page = page;
    _QueryString = querystring;
   }
   #endregion

   #region 析构函数
   ~URLRewrite(){
   }
   #endregion

  }

 }

 

 

 

}
 

标签:C#  

发表评论

必填

选填

选填

必填,不填不让过哦,嘻嘻。

记住我,下次回复时不用重新输入个人信息

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。