asp.net-mvc-4 – 为什么MVC4 @ Styles.Render()在调试模式下不
发布时间:2020-12-16 00:36:42 所属栏目:asp.Net 来源:网络整理
导读:我正在MVC4中实现捆绑和缩小支持,并设置它,因此可以为我自动编译我的Bootstrap .less文件。我的BundleConfig.cs文件中有以下代码 public class BundleConfig{ public static void RegisterBundles(BundleCollection bundles) { // base bundles that come
我正在MVC4中实现捆绑和缩小支持,并设置它,因此可以为我自动编译我的Bootstrap .less文件。我的BundleConfig.cs文件中有以下代码
public class BundleConfig { public static void RegisterBundles(BundleCollection bundles) { // base bundles that come with MVC 4 var bootstrapBundle = new Bundle("~/bundles/bootstrap").Include("~/Content/less/bootstrap.less"); bootstrapBundle.Transforms.Add(new TwitterBootstrapLessTransform()); bootstrapBundle.Transforms.Add(new CssMinify()); bundles.Add(bootstrapBundle); } } TwitterBootsrapLessTransform如下(它比我想要的更复杂,因为需要将sub.less文件导入到dotLess) public class TwitterBootstrapLessTransform : IBundleTransform { public static string BundlePath { get; private set; } public void Process(BundleContext context,BundleResponse response) { setBasePath(context); var config = new DotlessConfiguration(DotlessConfiguration.GetDefault()); config.LessSource = typeof(TwitterBootstrapLessMinifyBundleFileReader); response.Content = Less.Parse(response.Content,config); response.ContentType = "text/css"; } private void setBasePath(BundleContext context) { BundlePath = context.HttpContext.Server.MapPath("~/Content/less" + "/imports" + "/"); } } public class TwitterBootstrapLessMinifyBundleFileReader : IFileReader { public IPathResolver PathResolver { get; set; } private string basePath; public TwitterBootstrapLessMinifyBundleFileReader(): this(new RelativePathResolver()) { } public TwitterBootstrapLessMinifyBundleFileReader(IPathResolver pathResolver) { PathResolver = pathResolver; basePath = TwitterBootstrapLessTransform.BundlePath; } public bool DoesFileExist(string fileName) { fileName = PathResolver.GetFullPath(basePath + fileName); return File.Exists(fileName); } public byte[] GetBinaryFileContents(string fileName) { throw new System.NotImplementedException(); } public string GetFileContents(string fileName) { fileName = PathResolver.GetFullPath(basePath + fileName); return File.ReadAllText(fileName); } } 在我的基地_Layout.cshtml页面上,我尝试通过这样做来呈现css文件 @Styles.Render("~/bundles/bootstrap"); 如mvc tutorial所建议的,客户端浏览器最终请求的文件是 http://localhost:53729/Content/less/bootstrap.less 这会导致错误。如果我将以下链接放入基本布局页面,它按预期工作。 <link href="~/bundles/bootstrap" rel="stylesheet" type="text/css" /> 为什么@ Styles.Render()在调试模式下的行为方式相同?它在释放模式下工作。我可以理解你如何不想在调试中进行捆绑和缩小,但是如何强制这个捆绑包总是以同样的方式工作? 解决方法
所以基本上当debug =“true”时,脚本/样式渲染方法假设优化是关闭的,这意味着没有捆绑和没有缩小,这意味着它不会调用你的转换;相反,它只会渲染链接到包的原始内容(在您的情况下,这是boostrap.less)。
您可以覆盖此行为,并始终通过设置BundleTable.EnableOptimizations = true来运行优化。这将强制渲染方法始终进行捆绑/缩小。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 使用asp.net Web表单中的流返回可下载文件
- asp.net – Global.asax没有为Release版本触发
- asp.net-mvc – 为什么使用ASP.NET MVC局部视图(.ascx)
- asp.net – 获取当前用户的GUID?
- iis-7 – ASPError对象在我的自定义错误页面上不包含任何数
- asp.net-mvc – !ClrStack -a调用ASP.NET MVC应用程序显示
- 如何在本地运行ASP.NET C#Web应用程序?
- asp.net-mvc – asp.net mvc针对不同操作的不同验证规则
- asp.net – IE 8和客户端缓存
- 如何在ASP.Net中将HTML页面转换为图像格式
推荐文章
站长推荐
- asp.net-mvc – ASP.NET MVC:如何绑定List类型的
- asp.net-mvc – ASP.NET MVC忽略所有url结尾的“
- asp.net – asp文本框控件的CSS
- asp.net-mvc-4 – Web.API和FromBody
- asp.net – System.Web.AspNetHostingPermission
- asp.net-mvc – 根据MVC中第一个dropdownlistfor
- asp.net-mvc – 如何将Azure AD身份验证用于单独
- asp.net-mvc – 在MVC视图中,如果在表单标记中没
- asp.net-mvc – 使ASP.NET绑定指定media =屏幕的
- asp.net-core – 无法运行dnx控制台应用程序
热点阅读