!!!ASP.NETバンドルでの相対パスを使用
[ASP.NET.Core][ASP.NET]
!App_Start/BundleConfig.cs
using System.Configuration;
using System.Web;
using System.Web.Optimization;
namespace SalesMsReport
{
public class BundleConfig
{
// バンドルの詳細については、https://go.microsoft.com/fwlink/?LinkId=301862 を参照してください
public static void RegisterBundles(BundleCollection bundles)
{
bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
"~/Scripts/jquery-{version}.js"));
bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
"~/Scripts/jquery.validate*"));
// 開発と学習には、Modernizr の開発バージョンを使用します。次に、実稼働の準備が
// 運用の準備が完了したら、https://modernizr.com のビルド ツールを使用し、必要なテストのみを選択します。
bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(
"~/Scripts/modernizr-*"));
bundles.Add(new ScriptBundle("~/bundles/bootstrap").Include(
"~/Scripts/bootstrap.js"));
bundles.Add(new StyleBundle("~/Content/css").Include(
"~/Content/bootstrap.css",
"~/Content/site.css"));
// アプリケーションコンテキストパスを差し込む
var appCtx = ConfigurationManager.AppSettings["ApplicationContextPath"];
var transForm = new ApplicationContextPathTransform(appCtx);
foreach (var bundle in bundles.GetRegisteredBundles())
{
bundle.Transforms.Add(transForm);
}
}
}
public class ApplicationContextPathTransform : IBundleTransform
{
private string applicationContextPath;
public ApplicationContextPathTransform(string applicationContextPath)
{
this.applicationContextPath = applicationContextPath;
}
public void Process(BundleContext context, BundleResponse response)
{
if (!string.IsNullOrEmpty(this.applicationContextPath))
{
foreach (var file in response.Files)
{
// 指定されている場合、アプリケーションコンテキストパスの挿入
file.IncludedVirtualPath
= file.IncludedVirtualPath.Replace(@"~/", $@"~/{this.applicationContextPath}/");
}
}
}
}
}
!Web.config
発行時にWeb.configを差し替える設定が必要
*Web.config
*Web.Test.config
*Web.Release.config
!_Layout.cshtml
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/bootstrap")
!実行結果
*テスト
*本番