开发者

How can I identify the browser my app is using with MVC3

开发者 https://www.devze.com 2023-03-01 13:40 出处:网络
I would like to be able to make custom CSS to match the different browsers. What\'s the easy way to check which of the now most common browsers my user is u开发者_开发百科sing?

I would like to be able to make custom CSS to match the different browsers. What's the easy way to check which of the now most common browsers my user is u开发者_开发百科sing?

Hope someone out there has some up to date information for the later browsers.

Thanks,


Try using the browser object it can be used to create a helper like this, which can be extended to do almost anything you need:

public static HelperResult ForBrowser(this HtmlHelper html, string name, int[] majorVersions, Func<HttpBrowserCapabilitiesBase, HelperResult> template)
{
   var browser = html.ViewContext.HttpContext.Request.Browser;
   if (browser.Browser.ToLower() == name.ToLower() && 
       majorVersions.ToList().Contains(browser.MajorVersion))
       {
            return new HelperResult(writer => template(browser).WriteTo(writer));
       }
       return new HelperResult(writer => { });
}

And in your view:

   @Html.ForBrowser("Chrome", new[]{11,12}, @<div><h1>Its @item.Browser @item.MajorVersion!</h1></div> )

other versions of this helper:

    public static HelperResult ForBrowser(this HtmlHelper html, string name, int lowestMajorVersion, int highestMajorVersion, Func<HttpBrowserCapabilitiesBase, HelperResult> template)
{
     var browser = html.ViewContext.HttpContext.Request.Browser;
     if (browser.Browser.ToLower() == name.ToLower() &&
         browser.MajorVersion >= lowestMajorVersion &&
         browser.MajorVersion <= highestMajorVersion)
        {
           return new HelperResult(writer => template(browser).WriteTo(writer));
        }
       return new HelperResult(writer => { });
}

            public static HelperResult ForBrowsers(this HtmlHelper html, string[] names, Func<HttpBrowserCapabilitiesBase, HelperResult> template)
             {
                 var browser = html.ViewContext.HttpContext.Request.Browser;
                 return names.Any(x => x == browser.Browser.ToLower()) ? new HelperResult(writer => template(browser).WriteTo(writer)) : new HelperResult(writer => { });
             }

            public static HelperResult ForBrowser(this HtmlHelper html, string name, Func<HttpBrowserCapabilitiesBase, HelperResult> template)
             {
                 var browser = html.ViewContext.HttpContext.Request.Browser;
                 return browser.Browser.ToLower() == name.ToLower() ? new HelperResult(writer => template(browser).WriteTo(writer)) : new HelperResult(writer => { });
             }


            public static HelperResult BrowserInfo(this HtmlHelper html, Func<HttpBrowserCapabilitiesBase, HelperResult> template)
            {
                var browser = html.ViewContext.HttpContext.Request.Browser;
                return new HelperResult(writer => template(browser).WriteTo(writer));
            }


A good place to start would be the controller context.

HttpContext.Request.UserAgent 

You could sniff this for say iphone mozilla etc.

Steve sandersons mvc book shows you how to do this in an attribute form which is pretty cool. This is not 3 specific but I don't think they changed much in the context from version to version. You could also use jquery or javascript depending on what it is your looking to do once you detect the browser.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号