全局文件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;
    }

 

posted @ 2012-06-21 11:42  ComBat  阅读(1094)  评论(0编辑  收藏  举报