;

Code Snippets

I have said that I had to cobble this together in a hurry, well that mostly meant removing code that would not run on the server my site is currently running on. I do have a number of sample code fragments that I will eventually put on the site that show exotic process I have had to use to solve problems, but as a place holder for now, I will just put in a couple fragments that were what I had to remove to make this page work with this server

 

This is the code that allows me to use the User Agent String in the code to adjust the page produced, depending on if the User Agent String indicates the browser is on a Phone, Tablet or Desktop.

protected string GetDisplayModeId()
{
    foreach (var mode in DisplayModeProvider.Instance.Modes)
        if (mode.CanHandleContext(HttpContext))
            return mode.DisplayModeId;
}
            

 


 

This is the code that reads the User Agent String to control the routing and make the values available to the application code. This is a key part of the template I have put together.

public static void RegisterDisplayModes()
{
    DisplayModeProvider.Instance.Modes.Insert(0,new DefaultDisplayMode("Phone")
    {
        ContextCondition = (context => (
          (context.GetOverriddenUserAgent() != null) &&
          (
            (context.GetOverriddenUserAgent().IndexOf("iPhone", StringComparison.OrdinalIgnoreCase) >= 0) ||
            (context.GetOverriddenUserAgent().IndexOf("iPod", StringComparison.OrdinalIgnoreCase) >= 0) ||
            (context.GetOverriddenUserAgent().IndexOf("Droid", StringComparison.OrdinalIgnoreCase) >= 0) ||
            (context.GetOverriddenUserAgent().IndexOf("Blackberry", StringComparison.OrdinalIgnoreCase) >= 0) ||
            (context.GetOverriddenUserAgent().StartsWith("Blackberry", StringComparison.OrdinalIgnoreCase))
          )
        ))
    });
    DisplayModeProvider.Instance.Modes.Insert(0, new DefaultDisplayMode("Tablet")
    {
        ContextCondition = (context => (
          (context.GetOverriddenUserAgent() != null) &&
          (
            (context.GetOverriddenUserAgent().IndexOf("iPad", StringComparison.OrdinalIgnoreCase) >= 0) ||
            (context.GetOverriddenUserAgent().IndexOf("Playbook", StringComparison.OrdinalIgnoreCase) >= 0) ||
            (context.GetOverriddenUserAgent().IndexOf("Transformer", StringComparison.OrdinalIgnoreCase) >= 0) ||
            (context.GetOverriddenUserAgent().IndexOf("Kindle", StringComparison.OrdinalIgnoreCase) >= 0) ||
            (context.GetOverriddenUserAgent().IndexOf("Xoom", StringComparison.OrdinalIgnoreCase) >= 0)
          )
        ))
    });
}
            

 


 

This Server doesn't allow automatic Script Bundling and Minification. I had to quickly modify my code to work with it.

public static void RegisterBundles(BundleCollection bundles)
{
    //bundles.Add(new ScriptBundle("~/bundles/jquery").Include("~/Scripts/jquery-1.*"));
    bundles.Add(new ScriptBundle("~/bundles/jquery").Include("~/Scripts/jquery-2.*"));
    //bundles.Add(new ScriptBundle("~/bundles/jqueryui").Include("~/Scripts/jquery-ui*"));
    bundles.Add(new ScriptBundle("~/bundles/jqueryui").Include("~/Scripts/jquery-ui-1.10.3.js"));
    bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
                "~/Scripts/jquery.unobtrusive*",
                "~/Scripts/jquery.validate*"));
    bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(
                "~/Scripts/modernizr-*"));
    bundles.Add(new ScriptBundle("~/bundles/jquerymobile").Include("~/Scripts/jquery.mobile*"));
    bundles.Add(new StyleBundle("~/Content/css").Include("~/Content/site.css"));
    bundles.Add(new StyleBundle("~/Content/mobilecss").Include("~/Content/jquery.mobile*"));
    bundles.Add(new StyleBundle("~/Content/themes/base/css").Include(
                "~/Content/themes/base/jquery.ui.core.css",
                "~/Content/themes/base/jquery.ui.resizable.css",
                "~/Content/themes/base/jquery.ui.selectable.css",
                "~/Content/themes/base/jquery.ui.accordion.css",
                "~/Content/themes/base/jquery.ui.autocomplete.css",
                "~/Content/themes/base/jquery.ui.button.css",
                "~/Content/themes/base/jquery.ui.dialog.css",
                "~/Content/themes/base/jquery.ui.slider.css",
                "~/Content/themes/base/jquery.ui.tabs.css",
                "~/Content/themes/base/jquery.ui.datepicker.css",
                "~/Content/themes/base/jquery.ui.progressbar.css",
                "~/Content/themes/base/jquery.ui.theme.css"));
}