C# ASP.Net MVC4捆绑包,用于调试设置为false时未呈现的文件较少

C# ASP.Net MVC4捆绑包,用于调试设置为false时未呈现的文件较少,c#,asp.net-mvc-4,less,bundle,minify,C#,Asp.net Mvc 4,Less,Bundle,Minify,在一个简单的ASP.NETMVC4测试应用程序中,我安装了无点NuGet包和 我的.less文件被正确解析为CSS,并且在debug=true时工作正常 <link href="/Public/less/main.less" rel="stylesheet"/> <link href="/Public/less/home.less" rel="stylesheet"/> <link href="/Public/less/a.less" rel="stylesheet

在一个简单的ASP.NETMVC4测试应用程序中,我安装了无点NuGet包和

我的
.less
文件被正确解析为CSS,并且在
debug=true
时工作正常

<link href="/Public/less/main.less" rel="stylesheet"/>
<link href="/Public/less/home.less" rel="stylesheet"/>
<link href="/Public/less/a.less" rel="stylesheet"/>
<link href="/Public/less/b.less" rel="stylesheet"/>
<link href="/Public/less/c.less" rel="stylesheet"/>
这是我的包配置文件;同样,直接取自教程:

public class BundleConfig
{
    // For more information on Bundling, visit http://go.microsoft.com/fwlink/?LinkId=254725
    public static void RegisterBundles(BundleCollection bundles)
    {

        // Compile .less files and create a bundle for them.
        var lessBundle = new Bundle("~/Public/less").Include(
                                                        "~/Public/less/main.less",
                                                        "~/Public/less/home.less",
                                                        "~/Public/less/a.less",
                                                        "~/Public/less/b.less",
                                                        "~/Public/less/c.less");
        lessBundle.Transforms.Add(new LessTransform());
        lessBundle.Transforms.Add(new CssMinify());
        bundles.Add(lessBundle);
    }
}
在我的布局文件中:

<head>
    @Styles.Render("~/Public/less")
</head>


关于为什么bundle不能在
debug=false
上正常工作的任何想法?

除非我误解了你所说的没有意义

当您拥有
debug=true
时,您的样式表似乎没有被捆绑,因为它们被单独引用,因此没有缩小/捆绑到单个文件中,当debug设置为true时,这是预期的,这样您就可以轻松地调试脚本/样式,而不必费力地浏览缩小和捆绑的代码

当您使用
debug=false
时,似乎正在进行缩小/绑定,因为您只获得两个URL

看起来很奇怪的一件事是,less url没有与之关联的哈希值,即.v=为空,这可能是需要研究的内容


您还可以尝试在浏览器中加载url
/Public/less?v=
,并查看它返回的内容,如果缩小/绑定遇到问题,它通常会在文件顶部向您发出警告。

看起来您的文件空得更少。所以散列没有生成是正常的

只是想让你知道。您不需要为更少的文件使用bundle。您可以使用import关键字insinde less组合文件。
@输入“a.less”

MVC 4.0中的绑定和缩小默认情况下在调试模式下是禁用的,因为缩小和捆绑使得调试非常困难,甚至有时是不可能的。您可以通过在
LessTransform.Process
方法上设置断点来测试它
LessTransform.Process
仅在项目运行时使用
debug=false
BundleTable.EnableOptimizations=true
调用

表示捆绑的结果为空

请确保至少有一个较少的文件生成CSS内容,如果是,请检查
LessTransform
类,它必须是:

public class LessTransform : IBundleTransform
{
    void IBundleTransform.Process(BundleContext context, BundleResponse response)
    {
        response.Content = Less.Parse(response.Content);
        response.ContentType = "text/css";
    }
}

通过将
BundleTable.EnableOptimizations=true
在RegisterBundles方法中,您可以在调试模式下覆盖优化机制(捆绑和缩小)的默认设置,然后您可以调试并检查
Less.Parse(response.Content)的结果

.less
编译为
css
时,问题是一个无声错误


我使用断点检查我的
LessTransform
类,该类使用
dotless
库进行编译

public class LessTransform : IBundleTransform
{
    public void Process(BundleContext context, BundleResponse response)
    {
        response.Content = Less.Parse(response.Content); // Breakpoint here.
        response.ContentType = "text/css";
    }
}
我注意到,当鼠标悬停在
response.Content
上时,我可以看到我的
less
代码,但在
less.Parse
代码之后,
response.Content
将变为空

我通过检查器运行
less
代码,发现代码中有语法错误


一旦我修复了错误,绑定和缩小功能就如预期的那样正常工作。

你能试试这个答案吗:他使用BundleTransformer.Less软件包。这可能会帮助你…我的less文件不是空的,当
debug
设置为true时,它们工作正常。你为什么否决我?我给了你如何解决问题的建议,而你完全忽略了它……我仔细检查了
LessTransform
类,它内部有适当的代码,可以将less转换为css。还有其他想法吗?这是正确的答案,应该被接受,因为这正是OP在他自己发布的答案中所说的。我接受了这个答案,但值得一提的是
less
文件中的错误将无声地失败,并成为一个空包。这就是为什么我发布了我自己的答案——希望它能帮助别人。
public class LessTransform : IBundleTransform
{
    void IBundleTransform.Process(BundleContext context, BundleResponse response)
    {
        response.Content = Less.Parse(response.Content);
        response.ContentType = "text/css";
    }
}
public class LessTransform : IBundleTransform
{
    public void Process(BundleContext context, BundleResponse response)
    {
        response.Content = Less.Parse(response.Content); // Breakpoint here.
        response.ContentType = "text/css";
    }
}