2013-10-07 21 views
1

我在我的MVC应用程序中使用的引导较少。 捆绑,然后再缩小少,我用LessBundle(https://github.com/scott-xu/System.Web.Optimization.Less应用LessBundle和CssRewriteUrlTransform时解析图像时出错

var bundle = new LessBundle("~/css/home").Include(
      "~/_globalresources/Css/language.less"); 

LessBundle是工作,但不得不解决CSS图像URL的问题。所有图片网址都不正确。所以,我在这种情况下应用CssRewriteUrlTransform,但失败。所有图片网址也不正确。

var bundle = new LessBundle("~/css/home").Include(
      "~/_globalresources/Css/language.less", new CssRewriteUrlTransform()); 

我尝试使用StyleBundle而不是LessBundle重新检查CssRewriteUrlTransform和所有的图片网址解决不好。

var bundle = new StyleBundle("~/css/home").Include(
       "~/_globalresources/Css/language.less", new CssRewriteUrlTransform()); 

我想,使用LessBundle和CssRewriteUrlTransform都会给出错误的结果。

请帮我解决我的问题归档我的目的:捆绑较少&解决图像的URL。谢谢。

回答

0

我解决了我的问题。下载LessTransform并像这样修复:

public void Process(BundleContext context, BundleResponse bundle) 
     { 
      if (context == null) 
      { 
       throw new ArgumentNullException("context"); 
      } 

      if (bundle == null) 
      { 
       throw new ArgumentNullException("bundle"); 
      } 

      context.HttpContext.Response.Cache.SetLastModifiedFromFileDependencies(); 

      var lessParser = new Parser(); 
      var lessEngine = this.CreateLessEngine(lessParser); 

      var content = new StringBuilder(bundle.Content.Length); 

      var bundleFiles = new List<BundleFile>(); 

      foreach (var bundleFile in bundle.Files) 
      { 
       bundleFiles.Add(bundleFile); 
       var filePath = bundleFile.IncludedVirtualPath; 
       filePath = filePath.Replace('\\', '/'); 
       if (filePath.StartsWith("~")) 
       { 
        filePath = VirtualPathUtility.ToAbsolute(filePath); 
       } 

       if (filePath.StartsWith("/")) 
       { 
        filePath = HostingEnvironment.MapPath(filePath); 
       } 

       this.SetCurrentFilePath(lessParser, filePath); 
       var source = File.ReadAllText(filePath); 
       var transformContent = lessEngine.TransformToCss(source, filePath); 
       foreach (var transform in bundleFile.Transforms) 
       { 
        transformContent = transform.Process(bundleFile.IncludedVirtualPath, transformContent); 
       } 

       content.Append(transformContent); 
       content.AppendLine(); 

       bundleFiles.AddRange(this.GetFileDependencies(lessParser, bundleFile.VirtualFile)); 
      } 

      if (BundleTable.EnableOptimizations) 
      { 
       // include imports in bundle files to register cache dependencies 
       bundle.Files = bundleFiles.Distinct(); 
      } 

      bundle.ContentType = "text/css"; 
      bundle.Content = content.ToString(); 
     } 

解析图像效果很好。

+0

从System.Web.Optimization.Less项目开始,它看起来像一个提交来解决这个问题 –