带有Web Essentials的Visual Studio 2013.3,带有dotLess 1.4.1的ASP .NET MVC 5项目。尝试在一个简单的less文件中使用@import (less)
语句,只导入一个简单的css文件,导致NullReferenceException
加注。没有@import
指令的(less)
没有按预期解析 - 它只是被解压缩到结果较少的文件。 有没有办法解决这个问题?或者它只是dotLess的一个错误?
~/Content/
内部文件夹中创建一个简单的Site.less
文件,该文件仅导入默认的Site.css
文件:_
@import (less) "Site.css";
LessTransform
课程如下:_
using System.Web.Optimization;
public class LessTransform : IBundleTransform
{
public void Process(BundleContext context, BundleResponse response)
{
response.Content = dotless.Core.Less.Parse(response.Content);
response.ContentType = "text/css";
}
}
RegisterBundles
文件BundleConfig
内的BundleConfig.cs
方法中添加以下行:_
var lessBundle = new Bundle("~/Content/less").Include("~/Content/Site.less");
lessBundle.Transforms.Add(new LessTransform());
bundles.Add(lessBundle);
_Layout.cshtml
文件中删除(或只是注释掉)行:_
@Styles.Render("~/Content/css")
并添加以下行:
@Styles.Render("~/Content/less")
结果:NullReferenceException
课程中会引发LessTransform
:
response.Content = Less.Parse(response.Content);
有没有办法解决这个问题?或者它只是dotLess的一个错误?
_
@import (less) "Site.css";
到
@import (less) "./Site.css";
无济于事。
将{始终复制到Site.css
的输出目录无效。
将@import (less) "Site.css"
更改为@import "Site.css"
没有任何帮助,因为dotLess不会像我们预期的那样解析结果行。它只是复制该行而不进行任何处理。
Web Essentials 2013 for Update 3正确表示较少文件的编译版本。尽管如此,问题仍然存在。
答案 0 :(得分:0)
我不认为DotLess支持这种类型的导入@import (less) "Site.css";
它在第1版第1节中介绍,但是Dotless并不总是最新的。
只需导入css就可以正常工作,然后捆绑可以解决剩下的问题。
@import site.css
答案 1 :(得分:0)
您可能想尝试使用Web Essentials 2013。看看它是否可以在保存(和构建)时编译较少。
答案 2 :(得分:0)
如果使用DotGess与WebGrease和System.Web.Optimization之类的东西进行捆绑,请在此处查看LessTransform代码:
在编译和捆绑时,你需要做一些时髦的东西来让import命令工作。项目中还有一个LessBundle
定义,但是当我创建我的端口时,我只是声明了一个标准Bundle
并将变换应用到它。
摘录于此,截至2014年10月15日:
using System;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Web.Optimization;
using Web.Optimization.Common;
using dotless.Core.configuration;
namespace Web.Optimization.Bundles.Less
{
public class LessTransform : IBundleTransform
{
private readonly DotlessConfiguration _configuration;
public LessTransform(DotlessConfiguration configuration)
{
_configuration = configuration;
}
public LessTransform()
: this(DotlessConfiguration.GetDefaultWeb())
{ }
public void Process(BundleContext context, BundleResponse response)
{
var builder = new StringBuilder();
foreach (var file in response.Files)
{
var path =
context.HttpContext.Server.MapPath(
file.IncludedVirtualPath);
var fileInfo = new FileInfo(path);
if (!fileInfo.Exists)
{
continue;
}
var content = ResolveImports(fileInfo);
builder.AppendLine(
_configuration.Web
? dotless.Core.LessWeb.Parse(content, _configuration)
: dotless.Core.Less.Parse(content, _configuration));
}
response.ContentType = ContentType.Css;
response.Content = builder.ToString();
}
private static readonly Regex s_lessImportRegex =
new Regex("@import [\"|'](.+)[\"|'];", RegexOptions.Compiled);
private static string ResolveImports(FileInfo file)
{
var content = File.ReadAllText(file.FullName, Encoding.UTF8);
return s_lessImportRegex.Replace(
content,
match =>
{
var import = match.Groups[1].Value;
// Is absolute path?
Uri uri;
if (Uri.TryCreate(import, UriKind.Absolute, out uri))
{
return match.Value;
}
var path =
Path.Combine(
file.Directory.FullName,
import);
if (!File.Exists(path))
{
throw new ApplicationException(
string.Concat("Unable to resolve import ", import));
}
return match.Value.Replace(import, path);
});
}
}
}