Asp.net URL重写脚本

来源:互联网 发布:汉服淘宝店平价学生党 编辑:程序博客网 时间:2024/06/07 06:56
void context_BeginRequest(object sender, EventArgs e)    {        HttpApplication application = (HttpApplication)sender;        HttpContext context = application.Context;        HttpResponse response = context.Response;        string path = context.Request.Path;        string file = System.IO.Path.GetFileName(path);        //重写后的URL地址        Regex regex = new Regex("InfoLists-(\\d+).aspx", RegexOptions.Compiled);        Match match = regex.Match(file);        //如果满足URL地址重写的条件        if (match.Success)        {            string userId = match.Groups[1].Value;            string rewritePath = "InfoLists.aspx?id=" + userId;            //将其按照UserInfo.aspx?UserId=123这样的形式重写,确保能正常执行            context.RewritePath(rewritePath);        }        Regex regex1 = new Regex("InfoDetail-(\\d+).aspx", RegexOptions.Compiled);        Match match1 = regex1.Match(file);        //如果满足URL地址重写的条件        if (match1.Success)        {            string id = match1.Groups[1].Value;            string rewritePath = "InfoDetail.aspx?id="+ id;            //将其按照UserInfo.aspx?UserId=123这样的形式重写,确保能正常执行            context.RewritePath(rewritePath);        }        Regex regex2 = new Regex("InfoList-(\\d+)-(\\d+).aspx", RegexOptions.Compiled);        Match match2 = regex2.Match(file);        //如果满足URL地址重写的条件        if (match2.Success)        {            string parentid = match2.Groups[1].Value;            string id = match2.Groups[2].Value;            string rewritePath = "InfoList.aspx?parentid=" + parentid + "&id=" + id;            //将其按照UserInfo.aspx?UserId=123这样的形式重写,确保能正常执行            context.RewritePath(rewritePath);        }    }    #endregion

0 0