Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Using server-side detection with ASP.NET

From: Mobile Web Design & Development Fundamentals

Video: Using server-side detection with ASP.NET

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.

Using server-side detection with ASP.NET

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.

Show transcript

This video is part of

Image for Mobile Web Design & Development Fundamentals
Mobile Web Design & Development Fundamentals

46 video lessons · 24271 viewers

Joe Marini
Author

 
Expand all | Collapse all
  1. 2m 48s
    1. Welcome
      1m 2s
    2. Using the exercise files
      1m 46s
  2. 29m 25s
    1. Understanding the mobile context
      8m 5s
    2. A survey of mobile sites
      11m 44s
    3. Targeting mobile browsers
      4m 31s
    4. Previewing a complete mobile site
      5m 5s
  3. 27m 20s
    1. Designing for one web
      3m 43s
    2. Using well-defined web standards
      3m 45s
    3. Designing mobile-friendly pages
      3m 40s
    4. Being crisp, clean, and succinct
      5m 45s
    5. Minimizing input where possible
      6m 47s
    6. Focusing on the core scenarios
      3m 40s
  4. 1h 13m
    1. Installing the tools
      3m 52s
    2. Setting up a local web server
      9m 13s
    3. Installing device emulators
      17m 5s
    4. Using device emulators
      13m 9s
    5. Downloading Modernizr and Mobile Boilerplate
      6m 22s
    6. Building a first mobile web page
      5m 43s
    7. Developing mobile pages with desktop browsers
      8m 6s
    8. Exploring useful mobile web development resources
      10m 23s
  5. 53m 26s
    1. Reviewing mobile markup languages
      5m 10s
    2. Understanding content adaptation approaches
      10m 32s
    3. Controlling the viewport layout
      12m 50s
    4. Designing forms
      13m 30s
    5. Using CSS media queries
      11m 24s
  6. 1h 11m
    1. Detecting client capabilities with script
      10m 8s
    2. Caching information with local storage
      9m 16s
    3. Determining position with geolocation
      12m 52s
    4. Minimizing HTTP requests with data URLs
      7m 39s
    5. Understanding user agent detection
      9m 8s
    6. Using server-side detection with PHP
      6m 52s
    7. Using server-side detection with ASP.NET
      4m 54s
    8. Using HTML5 Boilerplate for mobile
      11m 6s
  7. 39m 22s
    1. Measuring performance
      7m 41s
    2. Creating full-screen web apps
      6m 30s
    3. Customizing the user interface
      5m 14s
    4. Detecting orientation changes
      3m 58s
    5. Detecting device movement
      5m 21s
    6. Using touch events
      10m 38s
  8. 47m 14s
    1. Taking a look at the finished site
      7m 40s
    2. Examining the header and background image style on the Home page
      10m 10s
    3. Examining the hover effect and layout styles on the Tours page
      6m 42s
    4. Examining mobile forms on the Contact page
      9m 45s
    5. Viewing and testing the mobile site on emulators
      8m 11s
    6. Viewing the site on devices
      4m 46s
  9. 2m 34s
    1. Goodbye
      2m 34s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed Mobile Web Design & Development Fundamentals.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

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.

Are you sure you want to delete this note?

No

Your file was successfully uploaded.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.