Asp.net-core-configuration
ASP.NET Core-構成
この章では、ASP.NET Coreプロジェクトに関連する構成について説明します。 ソリューションエクスプローラーには、Startup.csファイルが表示されます。 以前のバージョンのASP.NET Coreを使用したことがある場合、おそらくglobal.asaxファイルが表示されるはずです。これは、Webアプリケーションの起動中に実行するコードを作成できる場所の1つでした。
- また、アプリケーションの実行に必要なすべての構成パラメーターを含むweb.configファイルが表示されることを期待します。
- ASP.NET Coreでは、これらのファイルはすべてなくなり、構成の代わりにStartupコードがStartup.csから読み込まれます。
- ファイル内にはStartupクラスがあり、このクラスでは、アプリケーションを構成し、構成ソースを構成することもできます。
- Startup.csファイル*のデフォルトの実装は次のとおりです。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
namespace FirstAppDemo {
public class Startup {
//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 http://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, IHostingEnvironment env,
ILoggerFactory loggerFactory) {
loggerFactory.AddConsole();
if (env.IsDevelopment()) {
app.UseDeveloperExceptionPage();
}
app.Run(async (context) => {
await context.Response.WriteAsync("Hello World!");
});
}
}
}
Startupクラスには、ほとんどの作業を行う2つのメソッドがあります。 クラスのConfigureメソッドは、HTTP処理パイプラインを構築する場所です。
- これは、アプリケーションがリクエストに応答する方法を定義します。 現在、このアプリケーションはHello World! また、アプリケーションの動作を変更する場合は、このConfigureメソッドにコードを追加してパイプラインを変更する必要があります。
- たとえば、indexlファイルなどの静的ファイルを提供する場合は、Configureメソッドにコードを追加する必要があります。
- エラーページを作成したり、要求をASP.NET MVCコントローラーにルーティングしたりすることもできます。これらのシナリオは両方とも、このConfigureメソッドで何らかの作業を行う必要があります。
- Startupクラスには、* ConfigureServices()*メソッドも表示されます。 これは、アプリケーションのコンポーネントを構成するのに役立ちます。
現時点では、すべての応答に Hello World !というハードコーディングされた文字列があります。 文字列 文字列をハードコーディングする代わりに、表示したいテキストを知っているコンポーネントからこの文字列をロードします。
- この他のコンポーネントは、データベース、Webサービス、またはJSONファイルからそのテキストをロードする場合があります。正確にどこにあるかは関係ありません。
- このハードコードされた文字列を持たないように、シナリオを設定するだけです。
ソリューションエクスプローラーで、プロジェクトノードを右クリックし、[追加]→[新しいアイテム]を選択します。
左ペインで*インストール済み→コード*を選択し、中央ペインでJSONファイルを選択します。 このファイルを AppSettings.json と呼び、上記のスクリーンショットのように*追加*ボタンをクリックします。
Hello World!の代わりに、プログラムにファイルからテキストを読み取らせることもできます。 Startup.csの文字列。 * AppSettings.jsonファイル*に次のコードを追加しましょう。
{
"message": "Hello, World! this message is from configuration file..."
}
次に、Startup.csファイルからこのメッセージにアクセスする必要があります。 JSONファイルから上記のメッセージを読み取る Startup.cs ファイルの実装を次に示します。
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Hosting;
using Microsoft.AspNet.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Configuration;
namespace FirstAppDemo {
public class Startup {
public Startup() {
var builder = new ConfigurationBuilder()
.AddJsonFile("AppSettings.json");
Configuration = builder.Build();
}
public IConfiguration Configuration { get; set; }
//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 http://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) {
app.UseIISPlatformHandler();
app.Run(async (context) => {
var msg = Configuration["message"];
await context.Response.WriteAsync(msg);
});
}
//Entry point for the application.
public static void Main(string[] args) =7gt; WebApplication.Run<Startup>(args);
}
}
アプリケーションを実行してみましょう。 アプリケーションを実行すると、次の出力が生成されます。