Start learning with our library of video tutorials taught by experts. Get started
Viewers: in countries Watching now:
This course surveys the core principles and techniques essential to building web sites for mobile devices. Author Joe Marini introduces the mobile context, sheds light on its unique coding requirements, and discusses interface design techniques that enhance existing sites for mobile viewing. The course shows how to approach designing for mobile form factors such as smaller screens and finger-based interaction, along with how to incorporate CSS3 and HTML5 capabilities, such as geolocation, local storage, and media queries.
In this example, I am going to show how to use ASP.NET to examine an incoming user agent string and then figure out whether or not the incoming user agent is from a mobile browser or not. And this is going to be pretty similar to what I did for the PHP example. So here in my code I've got the DetectTest.aspx file-- this is from the Example files folder-- and I've also got the code behind file DetectTestASP.aspx.cs, and I have got my ExampleSnippets open as well. Scroll down to the Server-Side Detection part for ASP.NET.
So here in the ASP.NET file, we've got some text that describes the example, and then I have a paragraph here with a span that is going to act as a placeholder for what the user agent string is. And then in here I've got an h2 that's going to act as a container for the result string, which will indicate whether or not this is a mobile browser. So this is set up very similar to the way that I did the PHP example. Let's go take a look at the code that actually does this.
This is the C# code that is behind the DetectTest.ASP.x page. And in the Page Load handler, this is the function that gets called when the ASP page loads in the browser and is processed by the ASP.NET engine on the server. When this function is called, we are going to get the user agent from the incoming request. We have a Boolean variable named is mobile, which we initialized to false. We set the Inner Text of the uaString placeholder in the ASP page to wherever the user agent is, similar to what we did for PHP example.
And then based upon whether or not the isMobile variable evaluates to true or false, we set the Inner Text of the result placeholder to be, "This appears to be a mobile browser," or "Hmmm, not a mobile browser." So without changing anything, let's go ahead and run this in the emulators as it is. And as you can see in the emulators, here is the Android emulator, and it says, "Hmmm it's not a mobile browser," Same thing in Opera, and the same thing in Windows phone, and that's because that variable is just being set to false right now.
So we are going to go ahead and plug our logic in and see if we can get some different results here. What I am going to do now is go over to my ExampleSnippets and I am going to go ahead and copy the logic here and I am going to paste that in. So this is pretty much the same kind of approach that I was taking in the PHP example. What I'm doing is getting the user agent from the request.
This Request object here is a variable that's available to my page via ASP.NET. So the Request object has a UserAgent string on it, and I trim off any white space that happens to be on there, and then I convert it to all lowercase, so without to worry about any case conversions or case comparisons or anything like that. Then I set the InnerText of the ua string to wherever the user agent is, and then we just use the Contains function on the string that contains the UserAgent to see if it contains certain keywords, like ipod or iphone or android or opera mobi. And again, this is very similar to what we did for the PHP example.
So if the UserAgent string contains the keyword we were looking for then we simply set the isMobile variable to true, and then down here we see if the result is true, and if it is, we just put the right string into the container. So let's go ahead and save this, and now I am going to copy this back up. So I am going to copy this files into my web server, and we will replace them. And now let's go ahead and refresh each one of our browsers, so we'll refresh the Android one. And you can see the result is now detecting that this is a mobile browser. Same thing. Let's take a look at Opera.
You see Opera is now being properly detected because the Opera Mobi string is right there. And then finally, for Windows Phone, you can see that Windows Phone OS is there, along with IE mobile. So using ASP.NET, we were able to look at the UserAgent string and make a determination about whether or not the incoming user agent was coming from a mobile browser. Now in this case we were just setting a variable to be true if it was. Again, similar to PHP, we could have done some more advanced stuff like redirect the user to another page or provide some custom CSS code or whatever. The sky is really the limit on this.
But in a nutshell that's how you use ASP.NET to detect an incoming user agent from a mobile device.
There are currently no FAQs about Mobile Web Design & Development Fundamentals.
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.