Angular でちょっとしたサーバーとの通信をテストする時に、Cors エラーを回避するために使っています😅設定は簡単で、Startup.cs で UseCors メソッドを下記のように実装します。
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(); } }
コメント
[…] 参考:ASP.NET Core Web API クロスオリジンを許可する方法 https://watermargin.net/2019/01/27/asp-net-core-web-api-%e3%82%af%e3%83%ad%e3%82%b9%e3%82%aa%e3%83%a… […]