Tôi đang triển khai hỗ trợ gói và rút gọn trong MVC4 và thiết lập nó để có thể biên dịch các tệp Bootstrap .less tự động cho tôi. Tôi có đoạn code sau trong BundleConfig.cs tôi nộpTại sao MVC4 @ Styles.Render() không hoạt động như mong đợi ở chế độ gỡ lỗi
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);
}
}
Các TwitterBootsrapLessTransform là như sau (nó là phức tạp hơn tôi muốn bởi vì sự cần thiết phải nhập khẩu các file phụ .less vào dotLess)
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);
}
}
Trên trang cơ sở _Layout.cshtml của tôi, tôi đã cố gắng để làm cho các tập tin css bằng cách làm này
@Styles.Render("~/bundles/bootstrap");
như được đề xuất bởi các mvc tutorial nhưng file clien t browser kết thúc bằng yêu cầu là
http://localhost:53729/Content/less/bootstrap.less
gây ra lỗi. Nếu tôi đặt liên kết sau vào trang bố cục cơ sở, nó hoạt động như mong đợi.
<link href="~/bundles/bootstrap" rel="stylesheet" type="text/css" />
Tại sao @ Styles.Render không hoạt động giống như trong chế độ gỡ lỗi? Nó hoạt động trong chế độ phát hành. Tôi có thể hiểu làm thế nào bạn không muốn nó bundling và minifying trong gỡ lỗi nhưng làm thế nào tôi có thể buộc bó này để làm việc theo cùng một cách luôn luôn?
tôi thấy đoạn mã này rất hữu ích. Bạn nên cân nhắc tạo một bài đăng trên blog về cách bạn có Twitter Bootstrap và Dotless làm việc cùng nhau. – Junto
Cảm ơn, có lẽ khi tôi có thêm thời gian cho bản thân mình, tôi sẽ bắt đầu viết blog. – PlTaylor
@PITaylor Không quan tâm bạn đang thấy các loại lỗi sau trong đầu ra css: Không thể giảm thiểu. Trả về nội dung chưa được sửa đổi. (1381,2): lỗi thời gian chạy CSS1019: Mã thông báo không mong muốn, tìm thấy '{' ... – Junto