MVC HTMLHelper to know a mobile device
CSS is really powerful and you can do anything really with it a website, but sometime simplicity is the better way.
When developing a site now, you always need to think of mobile devices and sometime the layout needs a whole different layout between mobile and desktop.

The simplest and easiest way I found was using a HTML Helper to find out what type of device or even browser is viewing my site and then using this knowledge to drive the partial view paths.

I would have parietal view for a layout for desktop and another for mobile. I know duplication is not good, but you can use partial view to reduce the duplication and have the main logic within the root views. Then have different views for just the layout


public static class BrowserQueryHTMLHelper

public static string CustomLayoutViewPartialViewURL(this HtmlHelper html)
var browser = html.ViewContext.HttpContext.Request.Browser;

return "MobileLayoutView";

return "DesktopLayoutView";


@using MVCPortal.HtmlHelpers

@{Html.RenderPartial(Html.CustomLayoutViewPartialViewURL(), Model);}

Latest blogs

Buy me a coffeeBuy me a coffee
Created: 10/04/2017 Total Comment: 0


(Not Displayed)
Human validation
Enter 7099 number, before submitting to confirm your human
[Home] [All Blogs] [Contact] [Me]
Buy me a coffeeBuy me a coffee