これが私がしたことです:
Twitter Bootstrap Nuget モジュールを追加しました。
これを私の_Layout.cshtmlファイルに追加しました:
<link href="@System.Web.Optimization.BundleTable.Bundles.ResolveBundleUrl("~/Content/twitterbootstrap/less")" rel="stylesheet" type="text/css" />
「less」フォルダーの名前をtwitterbootstrapに変更したことに注意してください。
すべての less ファイルを「imports」というサブフォルダーに移動しました。ただし、 bootstrap.lessと (レスポンシブ デザインの場合) Respond.lessを除きます。
~/Content/twitterbootstrap/imports
web.config に次の構成を追加しました。
<add key="TwitterBootstrapLessImportsFolder" value="imports" />
2 つのクラスを作成しました (上記のクラスをわずかに変更):
using System.Configuration;
using System.IO;
using System.Web.Optimization;
using dotless.Core;
using dotless.Core.configuration;
using dotless.Core.Input;
namespace TwitterBootstrapLessMinify
{
public class TwitterBootstrapLessMinify : CssMinify
{
public static string BundlePath { get; private set; }
public override void Process(BundleContext context, BundleResponse response)
{
setBasePath(context);
var config = new DotlessConfiguration(dotless.Core.configuration.DotlessConfiguration.GetDefault());
config.LessSource = typeof(TwitterBootstrapLessMinifyBundleFileReader);
response.Content = Less.Parse(response.Content, config);
base.Process(context, response);
}
private void setBasePath(BundleContext context)
{
var importsFolder = ConfigurationManager.AppSettings["TwitterBootstrapLessImportsFolder"] ?? "imports";
var path = context.BundleVirtualPath;
path = path.Remove(path.LastIndexOf("/") + 1);
BundlePath = context.HttpContext.Server.MapPath(path + importsFolder + "/");
}
}
public class TwitterBootstrapLessMinifyBundleFileReader : IFileReader
{
public IPathResolver PathResolver { get; set; }
private string basePath;
public TwitterBootstrapLessMinifyBundleFileReader() : this(new RelativePathResolver())
{
}
public TwitterBootstrapLessMinifyBundleFileReader(IPathResolver pathResolver)
{
PathResolver = pathResolver;
basePath = TwitterBootstrapLessMinify.BundlePath;
}
public bool DoesFileExist(string fileName)
{
fileName = PathResolver.GetFullPath(basePath + fileName);
return File.Exists(fileName);
}
public string GetFileContents(string fileName)
{
fileName = PathResolver.GetFullPath(basePath + fileName);
return File.ReadAllText(fileName);
}
}
}
私の IFileReader の実装は、TwitterBootstrapLessMinify クラスの静的メンバー BundlePath を調べます。これにより、インポートで使用するベース パスを挿入できます。別のアプローチを取りたかったのですが (クラスのインスタンスを提供することでしたが、できませんでした)。
最後に、次の行を Global.asax に追加しました。
BundleTable.Bundles.EnableDefaultBundles();
var lessFB = new DynamicFolderBundle("less", new TwitterBootstrapLessMinify(), "*.less", false);
BundleTable.Bundles.Add(lessFB);
これにより、どこからインポートすればよいかわからないインポートの問題が効果的に解決されます。