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> 
相關問題