首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >为什么MVC4 @Styles.Render()在调试模式下的行为不符合预期

为什么MVC4 @Styles.Render()在调试模式下的行为不符合预期
EN

Stack Overflow用户
提问于 2012-06-25 21:31:44
回答 3查看 33.6K关注 0票数 18

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

代码语言:javascript
复制
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如下所示(由于需要将子.less文件导入到dotLess中,它比我想象的要复杂得多)

代码语言:javascript
复制
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文件。

代码语言:javascript
复制
@Styles.Render("~/bundles/bootstrap");

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

代码语言:javascript
复制
http://localhost:53729/Content/less/bootstrap.less

这会导致错误。如果我把下面的链接放到按基本布局的页面中,它就会按预期工作。

代码语言:javascript
复制
<link href="~/bundles/bootstrap" rel="stylesheet" type="text/css" />

为什么@Styles.Render()在调试模式下不能以同样的方式运行?它在发布模式下工作。我可以理解你为什么不希望它在调试中捆绑和缩小,但是我怎么才能强制这个捆绑包始终以相同的方式工作呢?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-06-26 22:46:40

我最终做的是在我的_Layout.cshtml中放入一条debug if语句,这样无论发生什么情况,包都会呈现。我并不热衷于将其作为一种解决方案,但目前它正在发挥作用。

代码语言:javascript
复制
@if (Context.IsDebuggingEnabled)
{
    <link href="~/bundles/bootstrap" rel="stylesheet" type="text/css" />
}
else
{
    @Styles.Render("~/bundles/bootstrap")
}
票数 10
EN

Stack Overflow用户

发布于 2012-06-30 06:46:17

因此,基本上当为debug="true"时,脚本/样式呈现方法假定优化是关闭的,这意味着没有捆绑和缩小,这意味着它不会调用您的转换;相反,它只会呈现到捆绑的原始内容的链接(在本例中是boostrap.less )。

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

票数 31
EN

Stack Overflow用户

发布于 2012-08-23 22:55:36

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

在web.config中:

代码语言:javascript
复制
   <handlers>
    <add name="dotless" path="*.less" verb="GET" type="dotless.Core.LessCssHttpHandler,dotless.Core" resourceType="File" preCondition="" />
    </handlers>
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11190339

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档