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

      一个网站中的页面为了SEO优化等原因,需要修改页面的地址,制作成伪地址,即不直接用aspx结尾的页面地址。伪地址方法如下:

      

    /// <summary>
    /// Url重写
    /// </summary>
    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
    
            strNewUrl = ResolveUrl(strRequestPath);
            if (!string.IsNullOrEmpty(strNewUrl))
            {
                if (!string.IsNullOrEmpty(strNewUrl))
                {
                    context.RewritePath(strNewUrl);
                }
            }
        }
    
    
        /// <summary>
        /// 解析用户请求URL
        /// </summary>
        /// <param name="strRequestPath">用户请求URL</param>
        /// <returns>false/true</returns>
        private string ResolveUrl(string strRequestPath)
        {
            string strRule = "^http://网址\\.com/list_([1-9])(_([1-3]))?/(p(\\d+)\\.html)?$";
    
            Regex regRule = new Regex(strRule, RegexOptions.IgnoreCase);
            Match matRule = regRule.Match(strRequestPath);
    
            if (!matRule.Success) return strRequestPath;  //如果未能匹配,则原样返回
    
            string strNewUrl = ConfigurationManager.AppSettings["ErrorPath"];
            //匹配成功
            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);
                return strNewUrl;
            }
            strNewUrl = "/List.aspx?CateID=" + strCateID + "&PubDate=" + strType + "&Page=" + strPage;
            return strNewUrl;
        }
    
    }

    具体如下:

    1. 实现 System.Web.IHttpModule接口;
      
      
    2. context.BeginRequest += new EventHandler(ReUrl_BeginRequest);

      解释如下:
      // 摘要:
      // 在 ASP.NET 响应请求时作为 HTTP 执行管线链中的第一个事件发生。
      public event EventHandler BeginRequest;

    3. 方法 ReUrl_BeginRequest
    4. 跳转规则:ResolveUrl方法

    上面的代码实现的功能如下:

      浏览器地址栏显示地址为:http://网址.com/list_3_2/p1.html  

      实际找到的页面是:http://网址.com/List.aspx?CateID=3&PubDate=2&Page=1

      即实现两个地址的映射;

    注意:在配置文件中<system.web>节点中,需要加入:

      

    <httpModules>
          <add name="HttpModule" type="UrlReWrite"/>
    </httpModules>

      

  • 相关阅读:
    bzoj 3438 小M的作物
    洛谷 1126 机器人搬重物
    hdu 4055 Number String
    顺序对齐
    codevs 1300 文件排版
    NOIP 2015 提高组 Day2
    poj 2484 A Funny Game
    51nod 1548 欧姆诺姆和糖果 (制约关系优化枚举)
    hdu 1907 John (anti—Nim)
    hdu 2516 取石子游戏 (斐波那契博弈)
  • 原文地址:https://www.cnblogs.com/wxh19860528/p/2856083.html
Copyright © 2011-2022 走看看