トップ 差分 一覧 ping ソース 検索 ヘルプ PDF RSS ログイン

ASP.NETバンドルでの相対パスを使用



目次



記事一覧

キーワード

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
 <appSettings>
   <add key="ApplicationContextPath" value=""/>
 </appSettings>
  • Web.Test.config
 <appSettings>
   <add key="ApplicationContextPath" value="/TestApp" xdt:Transform="Replace" xdt:Locator="Match(key)"/>
 </appSettings>

  • Web.Release.config
 <appSettings>
   <add key="ApplicationContextPath" value="/ReleaseApp" xdt:Transform="Replace" xdt:Locator="Match(key)"/>
 </appSettings>

_Layout.cshtml

   @Scripts.Render("~/bundles/jquery")
   @Scripts.Render("~/bundles/bootstrap")

実行結果

  • テスト
   <script src="/TestApp/Scripts/jquery-3.3.1.js"></script>
   <script src="/TestApp/Scripts/bootstrap.js"></script>
  • 本番
   <script src="/ReleaseApp/SalesMsReport/Scripts/jquery-3.3.1.js"></script>
   <script src="/ReleaseApp/SalesMsReport/Scripts/bootstrap.js"></script>



YAGI Hiroto (piroto@a-net.email.ne.jp)
twitter http://twitter.com/pppiroto

Copyright© 矢木 浩人 All Rights Reserved.