全局文件Global.asax伪静态URL重写

View Code
void Application_BeginRequest(object sender, EventArgs e)
    {
        //1
        Regex reg = new Regex(@"news_show(\d+).html");//访问静态网址
        if (reg.Match(HttpContext.Current.Request.Url.AbsolutePath).Success)//是否匹配
        {
            string id = reg.Match(HttpContext.Current.Request.Url.AbsolutePath).Groups[1].Value;
            HttpContext.Current.RewritePath("news_show.aspx?id=" + id);
            return;
        }
        //2
        reg = new Regex(@"aboutus(\d+).html");//访问静态网址
        if (reg.Match(HttpContext.Current.Request.Url.AbsolutePath).Success)//是否匹配
        {
            string id = reg.Match(HttpContext.Current.Request.Url.AbsolutePath).Groups[1].Value;
            HttpContext.Current.RewritePath("aboutus.aspx?id=" + id);
            return;
        }
        //3
        reg = new Regex(@"anli(\d+).html");//访问静态网址
        if (reg.Match(HttpContext.Current.Request.Url.AbsolutePath).Success)//是否匹配
        {
            string id = reg.Match(HttpContext.Current.Request.Url.AbsolutePath).Groups[1].Value;
            HttpContext.Current.RewritePath("anli.aspx?id=" + id);
            return;
        }
        //4
        reg = new Regex(@"Design(\d+).html");//访问静态网址
        if (reg.Match(HttpContext.Current.Request.Url.AbsolutePath).Success)//是否匹配
        {
            string id = reg.Match(HttpContext.Current.Request.Url.AbsolutePath).Groups[1].Value;
            HttpContext.Current.RewritePath("Design.aspx?id=" + id);
            return;
        }
        //5
        reg = new Regex(@"Need(\d+).html");//访问静态网址
        if (reg.Match(HttpContext.Current.Request.Url.AbsolutePath).Success)//是否匹配
        {
            string id = reg.Match(HttpContext.Current.Request.Url.AbsolutePath).Groups[1].Value;
            HttpContext.Current.RewritePath("Need.aspx?id=" + id);
            return;
        }
        
        //6
        reg = new Regex(@"network(\d+).html");//访问静态网址
        if (reg.Match(HttpContext.Current.Request.Url.AbsolutePath).Success)//是否匹配
        {
            string id = reg.Match(HttpContext.Current.Request.Url.AbsolutePath).Groups[1].Value;
            HttpContext.Current.RewritePath("network.aspx?id=" + id);
            return;
        }

        //7
        reg = new Regex(@"news(\d+).html");//访问静态网址
        if (reg.Match(HttpContext.Current.Request.Url.AbsolutePath).Success)//是否匹配
        {
            string id = reg.Match(HttpContext.Current.Request.Url.AbsolutePath).Groups[1].Value;
            HttpContext.Current.RewritePath("news.aspx?id=" + id);
            return;
        }

        //8
        reg = new Regex(@"photoshop(\d+).html");//访问静态网址
        if (reg.Match(HttpContext.Current.Request.Url.AbsolutePath).Success)//是否匹配
        {
            string id = reg.Match(HttpContext.Current.Request.Url.AbsolutePath).Groups[1].Value;
            HttpContext.Current.RewritePath("photoshop.aspx?id=" + id);
            return;
        }
        //9
        reg = new Regex(@"service(\d+).html");//访问静态网址
        if (reg.Match(HttpContext.Current.Request.Url.AbsolutePath).Success)//是否匹配
        {
            string id = reg.Match(HttpContext.Current.Request.Url.AbsolutePath).Groups[1].Value;
            HttpContext.Current.RewritePath("service.aspx?id=" + id);
            return;
        }
        //此部分为主要页面
        //string str = @"index|index.html|index.htm";
        //string strAspx = "index.aspx";
        reg = regexURL(reg, @"index|index.html|index.htm", "index.aspx");
        reg = regexURL(reg, @"aboutus|aboutus.html|aboutus.htm", "aboutus.aspx");
        reg = regexURL(reg, @"anli|anli.html|anli.htm", "anli.aspx");
        reg = regexURL(reg, @"contactus|contactus.html|contactus.htm", "contactus.aspx");
        reg = regexURL(reg, @"Design|Design.html|Design.htm", "Design.aspx");
        reg = regexURL(reg, @"Need|Need.html|Need.htm", "Need.aspx");
        reg = regexURL(reg, @"network|network.html|network.htm", "network.aspx");
        reg = regexURL(reg, @"news|news.html|news.htm", "news.aspx");
        reg = regexURL(reg, @"news_show|news_show.html|news_show.htm", "news_show.aspx");
        reg = regexURL(reg, @"photoshop|photoshop.html|photoshop.htm", "photoshop.aspx");
        reg = regexURL(reg, @"service|service.html|service.htm", "service.aspx");
    }

    private static Regex regexURL(Regex reg, string str, string strAspx)
    {
        reg = new Regex(str);
        //var mcc = reg.Match(HttpContext.Current.Request.Url.AbsolutePath);
        if (reg.Match(HttpContext.Current.Request.Url.AbsolutePath).Success)
        {
            HttpContext.Current.RewritePath(strAspx);
        }
        return reg;
    }

 

转载于:https://www.cnblogs.com/lijie007jh/archive/2012/06/21/2557506.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值