#author("2018-05-07T00:28:45+00:00","default:admin","admin")
#author("2018-10-11T04:06:29+00:00","default:admin","admin")
-[[ASP.NET Core MVC における構成ファイル appsettings.json からの値取得:https://qiita.com/hiromasa-masuda/items/d7e33b20d3eedee771f4]]
-[[ASP.NET Core で設定ファイル(appsettings.json)から設定を取得する:http://kuttsun.blogspot.jp/2017/10/aspnet-core-json.html]]
-[[ASP.NET Core MVC: 環境変数を使う:https://blog.hmatoba.net/Article/128]]
-[[ASP.NET Coreでappsettings.jsonを使いこなす:http://kikki.hatenablog.com/entry/2016/11/30/000000]]
-[[ASP.NET Core / IIS で設定に環境変数を使う:http://blog.modd.com/entry/2016/08/24/115343]]
-[[ASP.NET Core の AppSetting の構成方法:https://qiita.com/TsuyoshiUshio@github/items/f45700dc2e95e7c3992a]]
-[[ASP.NET Core 1.0 でオプションを柔軟に扱えるようになった話:http://blog.shibayan.jp/entry/20160529/1464456800]]

-[[ASP.NET CORE: STEP BY STEP GUIDE TO ACCESS APPSETTINGS.JSON IN WEB PROJECT AND CLASS LIBRARY:https://hassantariqblog.wordpress.com/2017/02/20/asp-net-core-step-by-step-guide-to-access-appsettings-json-in-web-project-and-class-library/]]

-[[Easy Configuration Binding in ASP.NET Core - revisited:https://weblog.west-wind.com/posts/2017/Dec/12/Easy-Configuration-Binding-in-ASPNET-Core-revisited]]

-[[Asp.NET Core のコンフィグをカスタムクラスから使う:https://qiita.com/TsuyoshiUshio@github/items/dcaaf197889cb1ac0cdc]]

-[[アプリケーション構成情報(appsettings.json)を読み取る方法:https://st40.xyz/one-run/article/378/]]
-[[ASP.NET Coreの設定変更を即時反映させる:http://dblog.athome.co.jp/entry/2018/02/01/120000]]
-[[ASP.NET Core のオプション パターン:https://docs.microsoft.com/ja-jp/aspnet/core/fundamentals/configuration/options?view=aspnetcore-2.1]]
-[[ASP.NET CORE CONFIGURATION TIPS:https://davidpine.net/blog/asp-net-core-configuration/]]

-[[IIS上のASP.NET Coreアプリのリクエストタイムアウト設定:https://qiita.com/t-hashimoto2192/items/cb9458fa805e0192d41a]]
-[[ASP.NET Coreを動かすためのIISの構築方法:https://qiita.com/taiga_takahari/items/7809c78393750c42e443]]
-[[web.config による IIS の構成:https://docs.microsoft.com/ja-jp/aspnet/core/host-and-deploy/iis/?view=aspnetcore-2.0&tabs=aspnetcore2x#configuration-of-iis-with-webconfig]]
-[[Application configuration:https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/iis/?view=aspnetcore-2.1&tabs=aspnetcore2x#application-configuration]]([[日本語訳:https://docs.microsoft.com/ja-jp/aspnet/core/host-and-deploy/iis/?view=aspnetcore-2.0&tabs=aspnetcore2x#application-configuration]])

*設定ファイル [#g6162c4a]
**appsettings.json [#q1f3d873]
 {
   "UserSettings": {
     "IsDemoMode": false,
     "DefaultUser": {
       "Name": "山田 太郎",
       "Age": 33
     }
   },
   "Logging": {
     "IncludeScopes": false,
     "LogLevel": {
       "Default": "Warning"
     }
   }
 }

**Web.config [#o501217a]
プロジェクトに web.config ファイルが含まれていない場合、そのファイルは
ASP.NET Core モジュールを構成するための正しい processPath と arguments を使
用して作成され、発行された出力に移行されます。

プロジェクトに web.config ファイルが含まれていない場合、そのファイルは
ASP.NET Core モジュールを構成するための正しい processPath と arguments を使
用して作成され、発行された出力に移行されます。 変換によりファイル内の IIS 構
成の設定が変わることはありません。

web.config ファイルは、アクティブな IIS モジュールを制御する追加の IIS 構成
設定を提供する可能性があります。 ASP.NET Core アプリを使用して要求を処理でき
る IIS モジュールの詳細については、IIS モジュールのトピックを参照してくださ
い。

Web SDK によって web.config ファイルが変換されないようにするため、
<IsTransformWebConfigDisabled> プロパティをプロジェクト ファイルで使用します


*設定の参照 [#lcf969a9]
**Startup.cs [#k004295e]
-Starup クラス内のスコープであれば、Configuration プロパティ経由で、値を取得できる
 public Startup(IConfiguration configuration)
 {
     Console.WriteLine(configuration.GetValue<string>("UserSettings:IsDemoMode"));
     Console.WriteLine(configuration.GetValue<string>("UserSettings:DefaultUser:Name"));
 
     Configuration = configuration;
 }

**Controller [#q3d265e5]
-[[アプリケーション構成情報(appsettings.json)を読み取る方法:https://st40.xyz/one-run/article/378/]]

***appsettings.jsonの例 [#v89e8025]
 {
     "UserName": "ユーザー名",
     "Password": "パスワード",
     "App":{
         "Window":{
             "Width": 800,
             "Height": 600
         }
     }
 }

***GetValue() で取得 [#i9c53261]
 public class HomeController : Controller 
 {
     private readonly Configuration _configuration;
  
     public HomeController(IConfiguration configuration)
     {
         _configuration = configuration;
     }
  
     public IActionResult Index()
     {
         var userName = _configuration.GetValue<string>("UserName");  // ユーザー名
         var password = _configuration.GetValue<string>("Password");  // パスワード
         return View();
     }
 }

***クラスにバインドして取得 [#i2dd429a]

***staticの共通メソッドから呼び出したい場合 [#u3c53cdf]
-staticメソッドを持つクラスを用意
 public class AppSettings
 {
     public static IConfiguration Configuration { get; set; }
  
     public static string UserName { get { return Configuration.GetValue<string>("UserName"); } }
     public static string Password { get { return Configuration.GetValue<string>("Password"); } }
 }

-Startup.cs
 public class Startup
 {
     public Startup(IConfiguration configuration)
     {
         Configuration = configuration;
         AppSettings.Configuration = configuration;      // 追加
     }
 }

-コントローラで取得
 public class HomeController : Controller
 {
     public IActionResult Index()
     {
         var userName = AppSettings.UserName;    // ユーザー名
         var password = AppSettings.Password;    // パスワード
         return View();
     }
 }

**Controller以外 [#ffe586e2]
-[[Asp.NET Core のコンフィグをカスタムクラスから使う:https://qiita.com/TsuyoshiUshio@github/items/dcaaf197889cb1ac0cdc]]
-[[ASP.NET Coreの設定変更を即時反映させる:http://dblog.athome.co.jp/entry/2018/02/01/120000]]

-IOptionsをコンストラクタの引数で取る

-起動時に読み込んだ値しか参照しないのであれば、上記の「staticの共通メソッドから呼び出したい場合」の方法で十分

*各種設定 [#of2b70d6]
-[[ASP.NET Core で Timeout を伸ばしたい場合:https://medium.com/@arichika/asp-net-core-extend-increase-timeout-on-iis-6273f612757f]]

*構成(v2.1以降) [#x7fefd21]
-[[ASP.NET Core の構成:https://docs.microsoft.com/ja-jp/aspnet/core/fundamentals/configuration/?view=aspnetcore-2.1]]


トップ   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS