使用asp.net和Application_AcquireRequestState网站重定向到移动网站 [英] site redirection to mobile site using asp.net and Application_AcquireRequestState

查看:138
本文介绍了使用asp.net和Application_AcquireRequestState网站重定向到移动网站的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在asp.net网站,并有它的移动版本。我想,如果用户键入这个移动:

I have website in asp.net and have its mobile version as well. I want that if user types this in mobile:

http://example.com/ViewVacancy.aspx?ID=8674

随后他被定向到:

http://m.example.com/vacancy.aspx?name=8674

我已经写了下面的code:

I have written following code:

protected void Application_AcquireRequestState(object sender, EventArgs e)
    {


        HttpContext context = HttpContext.Current;


        if (context.Request.Browser.IsMobileDevice)
        {

            string uri = context.Request.Url.Authority.ToString(); 

            if (context.Request.Url.PathAndQuery.ToLower().Contains("viewvacancy"))
            {
                string query = context.Request.Url.Query; 
                query = query.Replace("ID", "Name");
                if (uri.ToString().StartsWith("http"))
                {
                    Uri nmobileURi = new Uri(context.Request.Url.Scheme + "://" + "m.example.com/vacancy.aspx" + query);
                    Response.Redirect(nmobileURi.ToString());
                }               
            }
            else
            {
                Response.Redirect("http://m.example.com");
            }
        }
        if (context.Request.ServerVariables["HTTP_USER_AGENT"] != null)
        {

            var userAgent = HttpContext.Current.Request.UserAgent.ToLower();
            if (userAgent.Contains("iphone") || userAgent.Contains("ipod")) 
            {
                Response.Redirect("http://example.com/downloadapp.htm");
                // iPhone
            }

            else if (userAgent.Contains("android"))
            {
                Response.Redirect("http://example.com/downloadgoogleplay.htm");
            }

            //Create a list of all mobile types
            string[] mobiles =
            new[]
            { "midp", "j2me", "avant", "docomo",
            "novarra", "palmos", "palmsource",
            "240×320", "opwv", "chtml",
            "pda", "windows ce", "mmp/",
            "blackberry", "mib/", "symbian",
            "wireless", "nokia", "hand", "mobi",
            "phone", "cdm", "up.b", "audio",
            "SIE-", "SEC-", "samsung", "HTC",
            "mot-", "mitsu", "sagem", "sony"
            , "alcatel", "lg", "eric", "vx",
            "NEC", "philips", "mmm", "xx",
            "panasonic", "sharp", "wap", "sch",
            "rover", "pocket", "benq", "java",
            "pt", "pg", "vox", "amoi",
            "bird", "compal", "kg", "voda",
            "sany", "kdd", "dbt", "sendo",
            "sgh", "gradi", "jb", "dddi",
            "moto", "iphone"
            };

            //Loop through each item in the list created above
            //and check if the header contains that text
            foreach (string s in mobiles)
            {
                if (context.Request.ServerVariables["HTTP_USER_AGENT"].
                ToLower().Contains(s.ToLower()))
                {                   
                    string uri = context.Request.Url.Authority.ToString(); 

                    if (context.Request.Url.PathAndQuery.ToLower().Contains("viewvacancy"))
                    {
                        string query = context.Request.Url.Query; 
                        query = query.Replace("ID", "Name");
                        if (uri.ToString().StartsWith("http"))
                        {
                            Uri nmobileURi = new Uri(context.Request.Url.Scheme + "://" + "m.example.com/vacancy.aspx" + query);
                            Response.Redirect(nmobileURi.ToString());
                        }
                    }
                    else
                    {
                        Response.Redirect("http://m.example.com");
                    }
                }
            }
        }
    }

据工作时,用户键入

It is working when user types

http://example.com

所以用户被重定向到:

http://m.example.com

但是,当用户类型:

http://example.com/viewvacancy.aspx?id=8674

这不是重定向到:

http://m.example.com/vacancy.aspx?name=8674

请提出解决的办法。

推荐答案

问题是的与string.replace()

query = Regex.Replace(query, "id", "name", RegexOptions.IgnoreCase);

这篇关于使用asp.net和Application_AcquireRequestState网站重定向到移动网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆