ASP.NET Core Web API クロスオリジンを許可する方法

Angular でちょっとしたサーバーとの通信をテストする時に、Cors エラーを回避するために使っています😅設定は簡単で、Startup.cs で UseCors メソッドを下記のように実装します。

Startup.cs
public class Startup
{
...
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
// 追加
app.UseCors(builder =>
builder
.WithOrigins("http://localhost:4200")
.AllowAnyHeader()
.AllowAnyMethod());
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseMvc();
}
}

💬 コメント