我正在实现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