Asp.net URL重写脚本

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值