It seems that with every new iteration of Microsoft s .NET
Framework we get tools that make development and deployment of applications
even easier and more powerful. Of course, sometimes these technologies are
there, but lightly documented. I found this true of the browser configuration
files, hence the reason for this tutorial. For more on web application development with ASP.NET, see "HTML5 for the ASP.NET Developer
" and "
Create a Single-Page Application in ASP.NET MVC 4
What Are Browser Configuration Files?
If you wanted to deploy your Web application to mobile
devices in ASP.NET 1.x, you had to: a) try to figure out how the mobile toolkit
really worked; b) possibly modify your machine.config and its associated
Microsoft has greatly simplified this task with ASP.NET 2.0 with master pages and their associated Browser Definition files. ASP.NET 2.0 can properly render itself on around two-dozen browsers right out of the box. Each of these browsers definitions are defined in fairly straightforward XML files with a .browser file extension. By combining their definitions with master pages, you can tell your Web form to use a different master page based on which browser is being used.
<%@ Page Language="VB" MasterPageFile="~/HackingPSP_RegularBrowsers.master" SonyPSP:MasterPageFile="~/HackingPSP_PSPOptimized.master" AutoEventWireup="false" CodeFile="Home.aspx.vb" Inherits="Home" Title="Hacking the PSP by Auri Rahimzadeh - Home Page" %>
Figure 1: The master page s header.
Note that I have a standard MasterPageFile for all browsers other than PSP (I really don t care if someone browses the site with a phone; that s not my audience and I won t spend time optimizing for them) and my specific SonyPSP:MasterPageFile designation for users who come to me with a Sony PSP. That s almost all there is to it to get ASP.NET 2.0 to show your device-optimized content (in this case, PSP-optimized) once you re done with this tutorial, of course.
Browser designations aren t limited to selecting master pages. Individual controls in ASP.NET are browser-aware, as well. Consider this Label control:
As you can see, you can define control values for specific browsers. Check Microsoft s Web site for more details on which controls are browser-aware. (WARNING: Do not develop this on a production server until you re sure it works! Constantly recompiling the browser cache file may cause IIS to recycle or restart, which is a bad thing in production!)
Step 1: Build Your Optimized Master Pages
Obviously you want content to test. In my case, it was my entire Hacking the PSP Web site, which is for my book, Hacking the PSP (go figure). In Figure 2 you can see where I ve built a master page for regular browsers and a master page for PSPs.
Figure 2: A master page for regular browsers and a master page for PSPs (circled in red).
Recall that master pages are meant to separate content from page templates, so you can re-use your content by plugging it into content placeholders in master pages, which are basically template files. This way, you can use that same content on regular browsers and the PSP by keeping the same placeholder names in your content files, and they ll automatically populate in the appropriate places in the templates. There are some great books from Wrox, O Reilly, and others on how all this works, plus tons of online help from Microsoft and third-parties. If you haven t tried ASP.NET 2.0 s master pages capability yet, try it now you ll love it!
NOTE: When you build master page files, make sure the Content Placeholder controls have matching names across master pages that will be used to service different browsers. The mapping of which content goes where is contingent upon knowing where placeholders are; if a placeholder can t be found, an error may be thrown.
Figure 3 shows the master page for regular browsers; Figure 4 shows the PSP version. Because the PSP has limited screen real estate, I needed to limit the navigation to only include items that would be of interest to PSP Web surfers (and not require a lot of scrolling). This meant eliminating my AdRotator control, Google AdSense ads, the Buy the Book link, and other items. I also had to use a slightly different version of my CSS file to accommodate the PSP s unique characteristics. Note that on the PSP master page I call out the fact that it is PSP-optimized, both to inform the reader there is a full version of the site available and to show off a little bit.
<%@ Master Language="VB" CodeFile="HackingPSP_RegularBrowsers.master.vb" Inherits="HackingPSP_RegularBrowsers" %> ...removed for brevity...