How to know if the asp.net site is accessed from a mobile device or from a system/laptop/machine(windows,mac etc)

北战南征 提交于 2020-01-01 10:29:45

问题


I have developed a website using asp.net web developer 2010. I would like to redirect to different page if the site is accessed from mobile browser. And, different functionality for windows/mac/linux etc users.

How can I do this?

I referred some posts in some forums, and it is said that I need to depend on third party libraries.

How could I implement this without any third party libraries?

Any one Please help me out.

Any help is appreciated. Thanks!!


回答1:


The Request.Browser object properties are probably where it is at, these are all options that may be useful to you:

Request.Browser.IsMobileDevice
Request.Browser.MobileDeviceManufacturer
Request.Browser.MobileDeviceModel
Request.Browser.ScreenPixelsWidth
Request.Browser.SupportsXmlHttp

Was there a reason you wanted to avoid a 3rd party component?

We currently use a WURFL device database via 51Degrees.mobi - which works really well.




回答2:


You can use useragent in asp.net to verify the request are coming from mobile or desktop.

protected override void OnInit(EventArgs e)
        {
            string userAgent = Request.UserAgent;
            if (userAgent.Contains("BlackBerry")
              || (userAgent.Contains("iPhone") || (userAgent.Contains("Android"))))
            {
                //add css ref to header from code behind
                HtmlLink css = new HtmlLink();
                css.Href = ResolveClientUrl("~/mobile.css");
                css.Attributes["rel"] = "stylesheet";
                css.Attributes["type"] = "text/css";
                css.Attributes["media"] = "all";
                Page.Header.Controls.Add(css);
            }
        }


来源:https://stackoverflow.com/questions/19788159/how-to-know-if-the-asp-net-site-is-accessed-from-a-mobile-device-or-from-a-syste

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!