首页  ·  知识 ·  编程语言
.NET伪静态类
ws_hgo  http://blog.csdn.net/ws_hgo/  .NET  编辑:dezai  图片来源:网络
httpModules dd type="Discuz.Forum.HttpModule, Discuz.Forum" name="HttpModule" /


       
   

 

 

 

 



               path="/showforum-{0}-{1}.aspx"
          pattern = "/showforum-(\d+)(-(\d+))?.aspx"
          page="/showforum.aspx"
          querystring="forumid=$1^page=$3" />
              path="/showtopic-{0}-{1}.aspx"
          pattern = "/showtopic-(\d+)(-(\d+))?.aspx"
          page="/showtopic.aspx"
          querystring="topicid=$1^page=$3" />
              path="/userinfo-{0}.aspx"
          pattern = "/userinfo-(\d+)*.aspx"
          page="/userinfo.aspx"
          querystring="userid=$1" />
               path="/rss-{0}.aspx"
          pattern = "/rss(-(\d+))?.aspx"
          page="/rss.aspx"
          querystring="forumid=$2" />


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

namespace Discuz.Forum
{
    /**////


    /// HttpModule 的摘要说明。
    ///

    public class HttpModule : System.Web.IHttpModule
    {
        //public readonly static Mutex m=new Mutex();

        /**////


        /// 实现接口的Init方法
        ///

        ///
        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.Response.Write("");
                context.Response.Write("Discuz!NT Error:
");
                context.Response.Write("");
                context.Response.Write("");
                context.Response.End();
            }

        }

        /**////


        /// 实现接口的Dispose方法
        ///

        public void Dispose()
        {
        }

        /**////


        /// 重写Url
        ///

        /// 事件的源
        /// 包含事件数据的 EventArgs
        private void ReUrl_BeginRequest(object sender, EventArgs e)
        {
            BaseConfigInfo config = Providers.BaseConfigProvider.Instance();
            if (config == null)
                return;
            HttpContext context = ((HttpApplication)sender).Context;
            string forumPath = BaseConfigFactory.GetForumPath.ToLower();

            string requestPath = context.Request.Path.ToLower();

            if (requestPath.StartsWith(forumPath))
            {
                if (requestPath.Substring(forumPath.Length).IndexOf("/") == -1)
                {
                    // 当前样式id
                    string strTemplateid = ConfigFactory.GetDefaultTemplateID().ToString();
                    //string ttt = Utils.GetCookie("dnttemplateid");
                    if (Utils.InArray(Utils.GetCookie("dnttemplateid"), TemplateFactory.GetValidTemplateIDList()))
                    {
                        strTemplateid = Utils.GetCookie("dnttemplateid");
                    }

                    foreach (SiteUrls.URLRewrite url in SiteUrls.GetSiteUrls().Urls)
                    {
                        if (Regex.IsMatch(requestPath, url.Pattern, RegexOptions.Compiled|RegexOptions.IgnoreCase))
                        {
                            string newUrl = Regex.Replace(requestPath.Substring(context.Request.Path.LastIndexOf("/")), url.Pattern, url.QueryString, RegexOptions.Compiled|RegexOptions.IgnoreCase);
                   
                            context.RewritePath(forumPath + "aspx/" + strTemplateid + url.Page, string.Empty, newUrl);

                            return;
                        }
                    }
                    context.RewritePath(forumPath + "aspx/" + strTemplateid + requestPath.Substring(context.Request.Path.LastIndexOf("/")));
                }
               
                else if (requestPath.StartsWith(forumPath + "archiver/"))
                {
                    string path = requestPath.Substring(forumPath.Length + 8);
                    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);
                            return;
                        }
                    }
                    return;
                }
                else if (requestPath.StartsWith(forumPath + "tools/"))
                {
                    string path = requestPath.Substring(forumPath.Length + 5);
                    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 + "tools" + url.Page, string.Empty, newUrl);
                            return;
                        }
                    }
                    return;
                }

            }

       
        }

    }

 

    /**///////////////////////////////////////////////////////////////////////
    public class SiteUrls
    {
        内部属性和方法#region 内部属性和方法
        private static object lockHelper = new object();
        private static volatile SiteUrls instance = null;

        string SiteUrlsFile = HttpContext.Current.Server.MapPath(BaseConfigFactory.GetForumPath + "config/urls.config");
        private System.Collections.ArrayList _Urls;
        public System.Collections.ArrayList Urls
        {
            get
            {
                return _Urls;
            }
            set
            {
                _Urls = value;
            }
        }

        private System.Collections.Specialized.NameValueCollection _Paths;
        public System.Collections.Specialized.NameValueCollection Paths
        {
            get
            {
                return _Paths;
            }
            set
            {
                _Paths = value;
            }
        }
 
        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

        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());
        }

        /**////


        /// 输出URL示例
        ///

        ///
        ///
        public string Show(int id)
        {
            return string.Format(Paths["Show"], id);
        }


        public class URLRewrite
        {
            成员变量#region 成员变量
            private string _Name;
            public string Name
            {
                get
                {
                    return _Name;
                }
                set
                {
                    _Name = value;
                }
            }

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

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

            private string _QueryString;
            public string QueryString
            {
                get
                {
                    return _QueryString;
                }
                set
                {
                    _QueryString = value;
                }
            }
            #endregion

            构造函数#region 构造函数
            public URLRewrite(string name, string pattern, string page, string querystring)
            {
                _Name = name;
                _Pattern = pattern;
                _Page = page;
                _QueryString = querystring;
            }
            #endregion
        }

    }

}

 

本文作者:ws_hgo 来源:http://blog.csdn.net/ws_hgo/
CIO之家 www.ciozj.com 微信公众号:imciow
   
免责声明:本站转载此文章旨在分享信息,不代表对其内容的完全认同。文章来源已尽可能注明,若涉及版权问题,请及时与我们联系,我们将积极配合处理。同时,我们无法对文章内容的真实性、准确性及完整性进行完全保证,对于因文章内容而产生的任何后果,本账号不承担法律责任。转载仅出于传播目的,读者应自行对内容进行核实与判断。请谨慎参考文章信息,一切责任由读者自行承担。
延伸阅读