Tuesday, August 26, 2014

check if the request came from mobile or computer in asp.net

check if the request came from mobile or computer in asp.net

In this topic we are going to see how to check if the request came from mobile or computer in asp.net.

Introduction:

From the m-dot approach, you can choose to build different UIs for different devices, and from the RWD (Responsive web design) approach you can choose to have a common code base. So while developing ASP.NET web applications, you can have the same codebase (or at least as identical as possible) as far as server-side code is concerned. However, you develop different UIs when targeting different devices (that means .aspx markup for Web Forms and views for MVC).

Reason for detecting a device:

For example, if a Web Form is being accessed from a desktop computer, you may want to attach a master page that displays a three-column layout to the user. If it is being accessed from a smartphone, you may want to attach a master page that displays a single-column layout.

Detecting a Mobile Device in Web Forms:


 HttpBrowserCapabilities bc = Request.Browser;

If you are developing an ASP.NET Web Forms application, you can easily see whether the requesting device is a mobile device by using the Request.Browser object. The Request.Browser object, which is an instance of the HttpBrowserCapabilities class, gives you a lot of information about the capabilities of the requesting browser. In particular, the IsMobileDevice property of the Request.Browser object tells you whether the requesting device is a mobile device .

A few more properties of the Request.Browser object can also come in handy for developing mobile applications


Property Description
IsMobileDevice Returns true if the requesting browser is mobile device; false otherwise
MobileDeviceManufacturer Returns the name of the mobile device manufacturer (if known)
MobileDeviceModel Returns the model name of a mobile device (if it is known)
ScreenPixelsHeight Returns the height of the display in pixels
ScreenPixelsWidth Returns the width of the display in pixels
Following figure shows a sample Web Form that outputs the properties listed in Table and is being accessed using the Safari desktop browser and an iPhone (via the Safari browser emulator).


A Web Form accessed using the Safari desktop browser returns IsMobileDevice as False, whereas the same Web Form when accessed from an iPhone returns True.

No comments:

Post a Comment