我希望能够制作自定义CSS以匹配不同的浏览器。检查用户现在最常用的浏览器的简单方法是什么?
希望有人能够获得以后浏览器的最新信息。
谢谢,
答案 0 :(得分:3)
尝试使用浏览器对象,它可以用来创建这样的帮助器,可以扩展为几乎所有你需要的东西:
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 => { });
}
在你看来:
@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));
}
答案 1 :(得分:2)
一个好的起点是控制器上下文。
HttpContext.Request.UserAgent
你可以用iphone mozilla等来嗤之以鼻。
史蒂夫桑德森mvc书向你展示了如何以一种很酷的属性形式做到这一点。这不是特定的3,但我认为它们在不同版本的上下文中没有太大变化。您还可以使用jquery或javascript,具体取决于您在检测到浏览器后要执行的操作。