URL重写

http://localhost:37977/UrlWrite.ashx?id=9
URL重写成下面的访问方式,有利于SEO搜索引擎
http://localhost:37977/UrlWrite-8.ashx

实现方法(用正则表达式匹配获取当前请求的虚拟路径):

/// <summary>
/// 当一个请求过来的时候会被调用,html静态文件是iis直接把文件给到浏览器,不经过asp.net引擎处理
/// 所以不会调用Application_BeginRequest方法
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Application_BeginRequest(object sender, EventArgs e)
{
    File.AppendAllText(@"c:1.txt", DateTime.Now + "Application_BeginRequest" 
        + Context.Request.RawUrl + "
");
    //Context.RewritePath("HtmlPage1.html");
    //url重写
    Match match = Regex.Match(Context.Request.Path, @"^/UrlWrite-(d+).ashx$");
    if (match.Success)
    {
        string id = match.Groups[1].Value;
        Context.RewritePath("/UrlWrite.ashx?id=" + id);
    }
}
原文地址:https://www.cnblogs.com/genesis/p/5061123.html