Я реализую поддержку связывания и минимизации в MVC4 и настраиваю его так, чтобы он мог автоматически компилировать мои файлы Bootstrap .less для меня. У меня есть следующий код в моем файле BundleConfig.cs
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 выглядит следующим образом (он сложнее, чем мне хотелось бы из-за необходимости импортировать файлы sub.less в 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);
}
}
На моем базовом _Layout. cshtml Я попытался отобразить файлы css, выполнив это
@Styles.Render("~/bundles/bootstrap");
, как это предлагается в учебнике по mvc, но клиентский браузер в конечном итоге запрашивает файл
http://localhost:53729/Content/less/bootstrap.less
, что вызывает ошибку. Если я помещу следующую ссылку на страницу базового макета, она будет работать, как и ожидалось.
Почему @Styles.Render() не ведет себя так же в режиме отладки? Он работает в режиме выпуска. Я могу понять, как вы не хотите связывать и минимизировать его в отладке, но как я могу заставить этот пакет всегда работать одинаково?