我正在實現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()在調試模式下的行爲方式相同?它在發佈模式下工作。我可以理解你如何不想在捆綁和調試中進行縮減,但我如何強制這個捆綁包始終以相同的方式工作?
我發現這個代碼片斷非常有用的。你應該考慮寫一篇關於你如何讓Twitter Bootstrap和Dotless一起工作的博客文章。 – Junto
謝謝,也許當我有更多時間爲自己時,我會開始寫博客。 – PlTaylor
@PITaylor出於興趣,你在css輸出中看到以下類型的錯誤:縮小失敗。返回未分類的內容。 (1381,2):運行時錯誤CSS1019:意外的令牌,發現'{'... – Junto