asp.net-mvc-4 – 为什么MVC4 @ Styles.Render()在调试模式下不如预期的那样运行

前端之家收集整理的这篇文章主要介绍了asp.net-mvc-4 – 为什么MVC4 @ Styles.Render()在调试模式下不如预期的那样运行前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在MVC4中实现捆绑和缩小支持,并设置它,因此可以为我自动编译我的Bootstrap .less文件。我的BundleConfig.cs文件中有以下代码
  1. public class BundleConfig
  2. {
  3. public static void RegisterBundles(BundleCollection bundles)
  4. {
  5. // base bundles that come with MVC 4
  6.  
  7. var bootstrapBundle = new Bundle("~/bundles/bootstrap").Include("~/Content/less/bootstrap.less");
  8. bootstrapBundle.Transforms.Add(new TwitterBootstrapLessTransform());
  9. bootstrapBundle.Transforms.Add(new CssMinify());
  10. bundles.Add(bootstrapBundle);
  11. }
  12. }

TwitterBootsrapLessTransform如下(它比我想要的更复杂,因为需要将sub.less文件导入到dotLess)

  1. public class TwitterBootstrapLessTransform : IBundleTransform
  2. {
  3. public static string BundlePath { get; private set; }
  4.  
  5. public void Process(BundleContext context,BundleResponse response)
  6. {
  7. setBasePath(context);
  8.  
  9. var config = new DotlessConfiguration(DotlessConfiguration.GetDefault());
  10. config.LessSource = typeof(TwitterBootstrapLessMinifyBundleFileReader);
  11.  
  12. response.Content = Less.Parse(response.Content,config);
  13. response.ContentType = "text/css";
  14. }
  15.  
  16. private void setBasePath(BundleContext context)
  17. {
  18. BundlePath = context.HttpContext.Server.MapPath("~/Content/less" + "/imports" + "/");
  19. }
  20. }
  21.  
  22. public class TwitterBootstrapLessMinifyBundleFileReader : IFileReader
  23. {
  24. public IPathResolver PathResolver { get; set; }
  25. private string basePath;
  26.  
  27. public TwitterBootstrapLessMinifyBundleFileReader(): this(new RelativePathResolver())
  28. {
  29. }
  30.  
  31. public TwitterBootstrapLessMinifyBundleFileReader(IPathResolver pathResolver)
  32. {
  33. PathResolver = pathResolver;
  34. basePath = TwitterBootstrapLessTransform.BundlePath;
  35. }
  36.  
  37. public bool DoesFileExist(string fileName)
  38. {
  39. fileName = PathResolver.GetFullPath(basePath + fileName);
  40.  
  41. return File.Exists(fileName);
  42. }
  43.  
  44. public byte[] GetBinaryFileContents(string fileName)
  45. {
  46. throw new System.NotImplementedException();
  47. }
  48.  
  49. public string GetFileContents(string fileName)
  50. {
  51. fileName = PathResolver.GetFullPath(basePath + fileName);
  52.  
  53. return File.ReadAllText(fileName);
  54. }
  55. }

在我的基地_Layout.cshtml页面上,我尝试通过这样做来呈现css文件

  1. @Styles.Render("~/bundles/bootstrap");

mvc tutorial所建议的,客户端浏览器最终请求的文件

  1. http://localhost:53729/Content/less/bootstrap.less

这会导致错误。如果我将以下链接放入基本布局页面,它按预期工作。

  1. <link href="~/bundles/bootstrap" rel="stylesheet" type="text/css" />

为什么@ Styles.Render()在调试模式下的行为方式相同?它在释放模式下工作。我可以理解你如何不想在调试中进行捆绑和缩小,但是如何强制这个捆绑包总是以同样的方式工作?

解决方法

所以基本上当debug =“true”时,脚本/样式渲染方法假设优化是关闭的,这意味着没有捆绑和没有缩小,这意味着它不会调用你的转换;相反,它只会渲染链接到包的原始内容(在您的情况下,这是boostrap.less)。

您可以覆盖此行为,并始终通过设置BundleTable.EnableOptimizations = true来运行优化。这将强制渲染方法始终进行捆绑/缩小。

猜你在找的asp.Net相关文章