Url重写

例如:http://www.baidu.com/view.aspx?id=1  和http://www.baidu.com/view-1.aspx  访问的页面相同; 

只需要全局应用程序类的Application_BeginRequest中添加如下代码:

 protected void Application_BeginRequest(object sender, EventArgs e)
        {
            
//使用正则表达式判断格式; View-12.aspx
            Regex reg = new Regex(@".+View-(\d+).aspx");
            
//获取请求的路径进行匹配
            var match = reg.Match(Request.Url.AbsolutePath);
            
if (match.Success)  //如果匹配
            {
                
string id = match.Groups[1].Value;  //获取正则表达式中的第一个id
                HttpContext.Current.RewritePath("View.aspx?id=" + id);  //重写Url
            }
        }
原文地址:https://www.cnblogs.com/zhangchenliang/p/2053502.html