17

我正在实现MVC4中的捆绑和缩小支持,并对其进行设置,以便它可以自动编译我的Bootstrap .less文件。我在我的BundleConfig.cs下面的代码文件为什么MVC4 @ Styles.Render()在调试模式下的行为不如预期

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文件导入带点)

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页我试图通过这样

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

渲染CSS文件,由mvc tutorial但该文件的CLIEN建议t浏览器结束请求是

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

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

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

为什么@ Styles.Render()在调试模式下的行为方式相同?它在发布模式下工作。我可以理解你如何不想在捆绑和调试中进行缩减,但我如何强制这个捆绑包始终以相同的方式工作?

+0

我发现这个代码片断非常有用的。你应该考虑写一篇关于你如何让Twitter Bootstrap和Dotless一起工作的博客文章。 – Junto

+0

谢谢,也许当我有更多时间为自己时,我会开始写博客。 – PlTaylor

+0

@PITaylor出于兴趣,你在css输出中看到以下类型的错误:缩小失败。返回未分类的内容。 (1381,2):运行时错误CSS1019:意外的令牌,发现'{'... – Junto

回答

10

我最终做的是在我的_Layout.cshtml中放入一个debug if语句,这样无论如何bundle都会呈现。我并不认为这是一个解决方案,但它现在正在工作。

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

所以基本上时debug="true",脚本/风格渲染方法假定优化关闭,这意味着没有捆绑和不缩小,这意味着它不会打电话到您的变换;相反,它只会呈现链接到包的原始内容(在你的情况下是boostrap.less)。

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

+0

您可以将其设置为特定捆绑包吗? – PlTaylor

+0

目前没有这是一个全局开关,它打开/关闭您的整个应用程序的打包/封装 –

+1

我想编辑您的帖子,因为我认为该属性的名称不是EnalbeOptimizations而是EnableOptimizations。 StackOverflow不允许1个字符编辑... :-( –

4

我解决这个问题通过让带点服务.LESS文件

在web.config中:

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