【问题标题】:Why is MVC4 @Styles.Render() not behaving as expected in debug mode为什么 MVC4 @Styles.Render() 在调试模式下没有按预期运行
【发布时间】:2012-06-26 18:25:37
【问题描述】:

我正在 MVC4 中实现捆绑和缩小支持并对其进行设置,以便它可以自动为我编译我的 Bootstrap .less 文件。我的 BundleConfig.cs 文件中有以下代码

public class BundleConfig
{
    public static void RegisterBundles(BundleCollection bundles)
    {
        // base bundles that come with MVC 4

        var bootstrapBundle = new Bundle("~/bundles/bootstrap").Include("~/Content/less/bootstrap.less");
        bootstrapBundle.Transforms.Add(new TwitterBootstrapLessTransform());
        bootstrapBundle.Transforms.Add(new CssMinify());
        bundles.Add(bootstrapBundle);
    }
}

TwitterBootsrapLessTransform 如下(它比我想的要复杂,因为需要将 sub .less 文件导入 dotLess)

public class TwitterBootstrapLessTransform : IBundleTransform
{
    public static string BundlePath { get; private set; }

    public void Process(BundleContext context, BundleResponse response)
    {
        setBasePath(context);

        var config = new DotlessConfiguration(DotlessConfiguration.GetDefault());
        config.LessSource = typeof(TwitterBootstrapLessMinifyBundleFileReader);

        response.Content = Less.Parse(response.Content, config);
        response.ContentType = "text/css";
    }

    private void setBasePath(BundleContext context)
    {
        BundlePath = context.HttpContext.Server.MapPath("~/Content/less" + "/imports" + "/");
    }
}

public class TwitterBootstrapLessMinifyBundleFileReader : IFileReader
{
    public IPathResolver PathResolver { get; set; }
    private string basePath;

    public TwitterBootstrapLessMinifyBundleFileReader(): this(new RelativePathResolver())
    {
    }

    public TwitterBootstrapLessMinifyBundleFileReader(IPathResolver pathResolver)
    {
        PathResolver = pathResolver;
        basePath = TwitterBootstrapLessTransform.BundlePath;
    }

    public bool DoesFileExist(string fileName)
    {
        fileName = PathResolver.GetFullPath(basePath + fileName);

        return File.Exists(fileName);
    }

    public byte[] GetBinaryFileContents(string fileName)
    {
        throw new System.NotImplementedException();
    }

    public string GetFileContents(string fileName)
    {
        fileName = PathResolver.GetFullPath(basePath + fileName);

        return File.ReadAllText(fileName);
    }
}

在我的基本 _Layout.cshtml 页面上,我尝试通过这样做来呈现 css 文件

@Styles.Render("~/bundles/bootstrap");

正如mvc tutorial 所建议的,但客户端浏览器最终请求的文件是

http://localhost:53729/Content/less/bootstrap.less

这会导致错误。如果我将以下链接按基本布局页面放入,它将按预期工作。

<link href="~/bundles/bootstrap" rel="stylesheet" type="text/css" />

为什么@Styles.Render() 在调试模式下的行为方式不同?它在发布模式下工作。我可以理解您不希望它在调试中捆绑和缩小,但我如何才能强制此捆绑始终以相同的方式工作?

【问题讨论】:

  • 我发现这段代码 sn-p 非常有用。你应该考虑写一篇关于你如何让 Twitter Bootstrap 和 Dotless 协同工作的博客文章。
  • 谢谢,也许等我有更多的时间我会开始写博客。
  • @PITaylor 您是否感兴趣地在 css 输出中看到以下类型的错误: 缩小失败。返回未缩小的内容。 (1381,2): 运行时错误 CSS1019: Unexpected token, found '{'...
  • 我的 kendo min css 文件有问题。油脂猴缩小器似乎有点敏感。

标签: asp.net-mvc-4 bundling-and-minification asp.net-optimization


【解决方案1】:

所以基本上当debug="true" 时,脚本/样式渲染方法假定优化已关闭,这意味着没有捆绑和缩小,这意味着它不会调用您的转换;相反,它只会呈现指向捆绑包原始内容的链接(在您的情况下是 boostrap.less )。

您可以通过设置BundleTable.EnableOptimizations = true 覆盖此行为并始终运行优化。这将强制渲染方法始终进行捆绑/缩小。

【讨论】:

  • 你能把这个设置成一个特定的包吗?
  • 目前没有,这是一个全局开关,它可以在整个应用中打开/关闭捆绑/缩小
  • 我想编辑您的帖子,因为我认为该属性的名称不是 EnalbeOptimizations 而是 EnableOptimizations。 StackOverflow 不允许编辑 1 个字符... :-(
  • 我认为这个解决方案比选择的答案更优雅!
【解决方案2】:

我最终做的是在我的 _Layout.cshtml 中放置一个 debug if 语句,这样包无论如何都会呈现。我并不热衷于将它作为一种解决方案,但它现在正在发挥作用。

@if (Context.IsDebuggingEnabled)
{
    <link href="~/bundles/bootstrap" rel="stylesheet" type="text/css" />
}
else
{
    @Styles.Render("~/bundles/bootstrap")
}

【讨论】:

    【解决方案3】:

    我通过让 dotless 为 .less 文件提供服务来解决这个问题

    在 web.config 中:

       <handlers>
        <add name="dotless" path="*.less" verb="GET" type="dotless.Core.LessCssHttpHandler,dotless.Core" resourceType="File" preCondition="" />
        </handlers>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-09-27
      • 1970-01-01
      • 1970-01-01
      • 2022-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多