zoukankan      html  css  js  c++  java
  • 伪地址改进

    之前在http://www.cnblogs.com/wxh19860528/archive/2013/01/11/2856083.html中的伪地址中有些逻辑问题,特别是:

    if (!matRule.Success) return strRequestPath; //如果未能匹配,则原样返回  

    这样会导致匹配失败后的处理失效,如果一个地址不匹配,则会报黄页错误。

    改进后的代码如下:

    public class UrlReWrite :System.Web.IHttpModule
    {
    
        /// <summary>
        /// 实现接口IHttpModule的Init方法,绑定事件
        /// </summary>
        /// <param name="context"></param>
        public void Init(HttpApplication context)
        {
            context.BeginRequest += new EventHandler(ReUrl_BeginRequest);
        }
    
        /// <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 strRequestPath = context.Request.Url.AbsoluteUri.ToLower();   //请求Url
            string strNewUrl = string.Empty;  //解析后的url
    
            bool bMark = ResolveUrl(strRequestPath, ref strNewUrl);
            if (bMark)
            {
                if (!string.IsNullOrEmpty(strNewUrl))
                {
                    context.RewritePath(strNewUrl);
                }
            }
            else
            {
                context.Response.Redirect(strNewUrl);
            }
        }
    
    
        /// <summary>
        /// 解析用户请求URL
        /// </summary>
        /// <param name="strRequestPath">用户请求URL</param>
        /// <returns>false/true</returns>
        private bool ResolveUrl(string strRequestPath, ref string strNewUrl)
        {
            string strErrorPage = ConfigurationManager.AppSettings["ErrorPath"];
    
            string strRule = "^http://网址\\.com/list_([1-9])(_([1-3]))?/(p(\\d+)\\.htm)?";
    
            Regex regRule = new Regex(strRule, RegexOptions.IgnoreCase);
            Match matRule = regRule.Match(strRequestPath);
    
            if (matRule.Success)
            {
                //匹配成功
                string strCateID = string.Empty;  //栏目ID
                strCateID = matRule.Groups[1].ToString();
    
                string strType = string.Empty;    //时间类型:今天昨天更早
                strType = matRule.Groups[3].ToString();
                if (string.IsNullOrEmpty(strType)) strType = "1"; //代表今天
    
                string strPage = string.Empty;    //类型
                strPage = matRule.Groups[5].ToString();
                if (string.IsNullOrEmpty(strPage)) strPage = "1"; //第一页
    
                if (string.IsNullOrEmpty(strCateID) || string.IsNullOrEmpty(strType) || string.IsNullOrEmpty(strPage))
                {
                    LogTools.LogHelper.Write("Url重写,获取页面参数失败。" + strRequestPath, null);
                    strNewUrl = strErrorPage;
                    return false;
                }
                else
                {
                    strNewUrl = "/List.aspx?CateID=" + strCateID + "&PubDate=" + strType + "&Page=" + strPage;
                    return true;
                }
            }
            else
            {
                strNewUrl = strErrorPage;
    return false;
            }
    
        }  
    }

     注释:

      改进后,解析用户请求URL的ResolveUrl()方法返回值变为bool类型。

      这样,在ReUrl_BeginRequest方法中可以通过ResolveUrl()方法返回值来判断是否匹配成功。

      ResolveUrl()方法中,匹配成功则找到页面地址:strNewUrl = "/List.aspx?CateID=" + strCateID + "&PubDate=" + strType + "&Page=" + strPage;

                 匹配失败则:strNewUrl = strErrorPage;赋值错误页地址,通过读取配置文件获得;

      还要注意:匹配成功是用的是 context.RewritePath(strNewUrl),内部重写地址;  而匹配失败用的是:context.Response.Redirect(strNewUrl);

  • 相关阅读:
    【leetcode】704.BinarySearch
    【leetcode】75.Sort Colors
    MongoChef
    问题 工具的缺陷
    MongoDB
    SpringFox
    npm 包管理工具
    RESTful 设计工具和Web框架
    笔记_JSON
    jsoup: Java HTML Parser
  • 原文地址:https://www.cnblogs.com/wxh19860528/p/2866563.html
Copyright © 2011-2022 走看看