gpt4 book ai didi

c# - Site.less' 是物理路径,但应该是虚拟路径

转载 作者:行者123 更新时间:2023-11-30 13:02:15 24 4
gpt4 key购买 nike

我在我的 MVC 4 项目中使用 DotLess 和我在网上找到的 LessTransformer.cs。

using System;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Web.Optimization;
using MyNamespace.Infrastructure.Bundler;
using dotless.Core.configuration;

namespace MyNameSpace.Infrastructure.Transformers.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();

_configuration.MinifyOutput = false;
_configuration.ImportAllFilesAsLess = true;
_configuration.CacheEnabled = false;
_configuration.LessSource = typeof(VirtualFileReader);

foreach (var file in response.Files)
{
if (!File.Exists(file.FullName))
{
continue;
}

var content = ResolveImports(file);

builder.AppendLine((_configuration.Web) ?
dotless.Core.LessWeb.Parse(content, _configuration)
: dotless.Core.Less.Parse(content, _configuration));
}

response.ContentType = "text/css";
response.Content = builder.ToString();
}

private static readonly Regex SLessImportRegex =
new Regex("@import [\"|'](.+)[\"|'];", RegexOptions.Compiled);

private static string ResolveImports(FileInfo file)
{
var content = File.ReadAllText(file.FullName, Encoding.UTF8);

return SLessImportRegex.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);
});
}
}
}

我已经添加了这个

_configuration.MinifyOutput         = false;
_configuration.ImportAllFilesAsLess = true;
_configuration.CacheEnabled = false;
_configuration.LessSource = typeof(VirtualFileReader);



using System.IO;
using System.Web.Hosting;
using dotless.Core.Input;

namespace MyNamespace.Infrastructure.Bundler
{
internal sealed class VirtualFileReader : IFileReader
{
public byte[] GetBinaryFileContents(string fileName)
{
fileName = GetFullPath(fileName);
return File.ReadAllBytes(fileName);
}

public string GetFileContents(string fileName)
{
fileName = GetFullPath(fileName);
return File.ReadAllText(fileName);
}

public bool DoesFileExist(string fileName)
{
fileName = GetFullPath(fileName);
return File.Exists(fileName);
}

private static string GetFullPath(string path)
{
return HostingEnvironment.MapPath("\\Content\\css\\" + path);
}
}
}

我有一个 Less 文件

@import url('LESS\Site.less');
@import url('LESS\Site.Main.less');

body {
#featured-posts {
clear:both;
}
}

@media all and (max-width:320px) {
@import url('LESS\Site.Mobile.less');
}

(我尝试了各种导入路径和/w 和/wo 函数符号 url()

在我的 BundleConfig 中全部拉取

    Bundle siteStyles = new      StyleBundle("~/bundles/css").Include("~/Content/css/Main.less");
siteStyles.Transforms.Add(new LessTransform());
siteStyles.Transforms.Add(new CssMinify());
bundles.Add(siteStyles);

在我添加 VirtualFileReader 之前,我遇到了错误:-

C:/SourceControl/Git/MyNameSpace/MyNameSpace/Content/css/LESS/Site.less' 是物理路径,但应该是虚拟路径。

自从我添加它后,我得到的只是 dotless.Core.LessWeb.Parse(content, _configuration) 上的值为 null

它总是在这条线上炸毁,虽然我一直有点狂热地实现这些更少的东西和无点,因为我以前没有使用过,但我似乎找不到任何好的可靠文档/教程在上面。因此,如果您能帮助我解决我的错误,并为我指明有关该主题的一些优秀教程的方向,我将不胜感激。

[编辑] 添加关于 less 文件和文件结构的详细信息。

My File structure is 

Content/css/Main.less
Content/css/LESS/Site.less
Content/css/LESS/Mobile.less

等等...TIA。

附注不要担心 MyNamespace/MyNamespace 内容,为了清楚起见,我只是将其全部缩短。

最佳答案

我不明白你为什么要使用自定义转换器。

LESS 规范说,如果您使用以下语法,文件将被自动捆绑:

@import "LESS/Site";

它非常适合我,我不需要任何自定义转换器,只需要标准的无点包。要启用缩小,您需要在 web.config 中进行此设置:

<dotless minifyCss="true" cache="true" web="true" />

我有大约 30 个组件和子组件的导入,输出是一个缩小的文件。

我建议你不要使用样式包,而是直接指向你的 less 文件:

<link href="~/Content/css/main.less" rel="stylesheet" />

dotless http 处理程序将启动,将 less 转换为 css 并将其缓存以供后续请求使用

关于c# - Site.less' 是物理路径,但应该是虚拟路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15882193/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com