| ページ一覧 | ブログ | twitter |  書式 | 書式(表) |

MyMemoWiki

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

提供: MyMemoWiki
ナビゲーションに移動 検索に移動

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

ASP.NET.Core | ASP.NET |

App_Start/BundleConfig.cs

  1. using System.Configuration;
  2. using System.Web;
  3. using System.Web.Optimization;
  4.  
  5. namespace SalesMsReport
  6. {
  7. public class BundleConfig
  8. {
  9. // バンドルの詳細については、https://go.microsoft.com/fwlink/?LinkId=301862 を参照してください
  10. public static void RegisterBundles(BundleCollection bundles)
  11. {
  12. bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
  13. "~/Scripts/jquery-{version}.js"));
  14.  
  15. bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
  16. "~/Scripts/jquery.validate*"));
  17.  
  18. // 開発と学習には、Modernizr の開発バージョンを使用します。次に、実稼働の準備が
  19. // 運用の準備が完了したら、https://modernizr.com のビルド ツールを使用し、必要なテストのみを選択します。
  20. bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(
  21. "~/Scripts/modernizr-*"));
  22.  
  23. bundles.Add(new ScriptBundle("~/bundles/bootstrap").Include(
  24. "~/Scripts/bootstrap.js"));
  25.  
  26. bundles.Add(new StyleBundle("~/Content/css").Include(
  27. "~/Content/bootstrap.css",
  28. "~/Content/site.css"));
  29.  
  30. // アプリケーションコンテキストパスを差し込む
  31. var appCtx = ConfigurationManager.AppSettings["ApplicationContextPath"];
  32. var transForm = new ApplicationContextPathTransform(appCtx);
  33.  
  34. foreach (var bundle in bundles.GetRegisteredBundles())
  35. {
  36. bundle.Transforms.Add(transForm);
  37. }
  38. }
  39. }
  40.  
  41. public class ApplicationContextPathTransform : IBundleTransform
  42. {
  43. private string applicationContextPath;
  44. public ApplicationContextPathTransform(string applicationContextPath)
  45. {
  46. this.applicationContextPath = applicationContextPath;
  47. }
  48. public void Process(BundleContext context, BundleResponse response)
  49. {
  50. if (!string.IsNullOrEmpty(this.applicationContextPath))
  51. {
  52. foreach (var file in response.Files)
  53. {
  54. // 指定されている場合、アプリケーションコンテキストパスの挿入
  55. file.IncludedVirtualPath
  56. = file.IncludedVirtualPath.Replace(@"~/", $@"~/{this.applicationContextPath}/");
  57. }
  58. }
  59. }
  60. }
  61. }

Web.config

発行時にWeb.configを差し替える設定が必要

  • Web.config
  1. <appSettings>
  2. <add key="ApplicationContextPath" value=""/>
  3. </appSettings>
  • Web.Test.config
  1. <appSettings>
  2. <add key="ApplicationContextPath" value="/TestApp" xdt:Transform="Replace" xdt:Locator="Match(key)"/>
  3. </appSettings>
  1. <appSettings>
  2. <add key="ApplicationContextPath" value="/ReleaseApp" xdt:Transform="Replace" xdt:Locator="Match(key)"/>
  3. </appSettings>

_Layout.cshtml

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

実行結果

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