C# 应用LessBundle和CSSRWriteUrlTransform时解析图像时出错

C# 应用LessBundle和CSSRWriteUrlTransform时解析图像时出错,c#,asp.net-mvc,less,bundle,C#,Asp.net Mvc,Less,Bundle,我在MVC应用程序中使用的是无引导。 为了减少捆绑和缩小,我使用了LessBundle LessBundle正在工作,但在CSS中解析图像URL时遇到问题。所有图像URL都不正确。因此,我在本例中应用了CSSRWriteUrlTransform,但失败了。所有图像URL也不正确 var bundle = new LessBundle("~/css/home").Include( "~/_globalresources/Css/language.less", new Css

我在MVC应用程序中使用的是无引导。 为了减少捆绑和缩小,我使用了LessBundle

LessBundle正在工作,但在CSS中解析图像URL时遇到问题。所有图像URL都不正确。因此,我在本例中应用了CSSRWriteUrlTransform,但失败了。所有图像URL也不正确

var bundle = new LessBundle("~/css/home").Include(
            "~/_globalresources/Css/language.less", new CssRewriteUrlTransform());
我尝试使用StyleBundle而不是LessBundle来重新检查CSSRWriteUrlTransform,所有图像URL都得到了很好的解析

var bundle = new StyleBundle("~/css/home").Include(
                "~/_globalresources/Css/language.less", new CssRewriteUrlTransform());
我认为,同时使用LessBundle和CSSRWriteUrlTransform会给出错误的结果


请帮助我解决我的问题,以归档我的目的:减少捆绑并解析图像URL。谢谢。

我解决了我的问题。下载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();
        }

解析图像效果很好。

似乎已经向System.Web.Optimization.Less项目提交了解决此问题的建议
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();
        }