zoukankan      html  css  js  c++  java
  • 使用Global.asax实现ASP.Net的URL重写

       URL重写也就是让动态页面伪装成“静态页面”,一方面可以隐藏真实的页面文件地址,另一方面对被搜索引擎的也有好处。(在不支持URLWRITE.NET组件的空间里面这是最好的方法)

        在ASP.NET中,使用Global.asax可以轻易的实现这个目的,原理简单,具体的代码一看便知:

    代码
            protected void Application_BeginRequest(Object sender, EventArgs e)
            {
                
    //article_detail.aspx?id=1 TO article_1.aspx
                string oldString = @"(.*)/article_(\d+)\.aspx";
                
    string newString = @"article_detail.aspx?id=$2";
                System.Text.RegularExpressions.Regex oReg 
    = new System.Text.RegularExpressions.Regex(oldString);
                
    if(oReg.IsMatch(Request.Url.ToString()))
                {
                    
    string ReWriteUrl = oReg.Replace(Request.Url.ToString(),newString);
                    HttpContext.Current.RewritePath(ReWriteUrl);
                }
                oReg 
    = null;
            }
  • 相关阅读:
    day25:接口类和抽象类
    vue1
    How the weather influences your mood?
    机器学习实验方法与原理
    How human activities damage the environment
    Slow food
    Brief Introduction to Esports
    Massive open online course (MOOC)
    Online learning in higher education
    Tensorflow Dataset API
  • 原文地址:https://www.cnblogs.com/hantianwei/p/1762606.html
Copyright © 2011-2022 走看看