従来の.NET Framework のASP.NET アプリケーションで設定情報を読み込む場合は Web.Configファイルから読み出すのが一般的な方法です。
.NET Framework のASP.NET アプリケーションで、Web.Configから情報を読み出す方法はこちらの記事を参照してください。
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*"
}
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*",
"AppConfiguration": {
"Value1": "Penguin Cookie",
"Value2": "ぺんぎんクッキー",
"Value3": "CD-A100"
}
}
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
namespace AppSettingsAspNetCore
{
public class Startup
{
private readonly IConfiguration _configuration;
public Startup(IConfiguration configuration)
{
_configuration = configuration;
}
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment()) {
app.UseDeveloperExceptionPage();
}
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapGet("/", async context =>
{
context.Response.ContentType = "text/plain; charset=utf-8";
string value1 = _configuration.GetSection("AppConfiguration")["Value1"];
string value2 = _configuration.GetSection("AppConfiguration")["Value2"];
string value3 = _configuration.GetSection("AppConfiguration")["Value3"];
await context.Response.WriteAsync("Hello World!\r\n");
await context.Response.WriteAsync("value1:" + value1 + "\r\n");
await context.Response.WriteAsync("value2:" + value2 + "\r\n");
await context.Response.WriteAsync("value3:" + value3 + "\r\n");
});
});
}
}
}
private readonly IConfiguration _configuration;
public Startup(IConfiguration configuration)
{
_configuration = configuration;
}
下記コードはコンテンツのエンコーディングとMIMEタイプを指定しています。このコードが無いとWebブラウザでの文字化けが発生します。
詳しくはこちらの記事を参照してください。context.Response.ContentType = "text/plain; charset=utf-8";
GetSection()
メソッドを呼び出して、AppConfiguration セクションの各値を取得しています。 string value1 = _configuration.GetSection("AppConfiguration")["Value1"];
string value2 = _configuration.GetSection("AppConfiguration")["Value2"];
string value3 = _configuration.GetSection("AppConfiguration")["Value3"];
await context.Response.WriteAsync("Hello World!\r\n");
await context.Response.WriteAsync("value1:" + value1 + "\r\n");
await context.Response.WriteAsync("value2:" + value2 + "\r\n");
await context.Response.WriteAsync("value3:" + value3 + "\r\n");