本文介绍了如何根据浏览器版本捆绑特定CSS?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用MVC4 StyleBundle 捆绑了一堆的CSS。有一个CSS,这是仅用于IE 9或更低。

I'm using MVC4 StyleBundle to bundle up a bunch of CSS. There is one CSS that's only needed for IE 9 or lower.

在我的 BundleConfig RegisterBundles 的方法,我有:

In my BundleConfig class of RegisterBundles method, I have:

if (HttpContext.Current.Request.Browser.Browser.Trim().ToUpperInvariant().Equals("IE") && HttpContext.Current.Request.Browser.MajorVersion <= 9)
    cssBundle.Include("~/Content/ie.css");

但后来我得到了一个请求不可用在这种情况下错误。它是不可能 RegisterBundles 方法的过程中检测浏览器?

But then I got a Request is not available in this context error. Is it not possible to detect browsers during RegisterBundles method?

推荐答案

没错TEJS是正确的。束是全局的,不能有所不同根据请求,因为它们是在第一次被引用后在服务器上高速缓存。所以,你在上面做什么的问题上,是根据什么浏览器第一次命中的请求,将填充缓存,并确定所有后续请求将收到,不管他们是否是IE9与否。

Yep Tejs is correct. Bundles are global and cannot vary based on request because they are cached on the server after being referenced for the first time. So the issue with what you are doing above, is that depending on what browser hits the request first, that will populate the cache and determine what all subsequent requests will recieve, regardless of whether they are IE9 or not.

这篇关于如何根据浏览器版本捆绑特定CSS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 21:43