在Startup檔案的ConfigureServices函式里注入服務
public void ConfigureServices(IServiceCollection services) { #region Cors跨域請求 services.AddCors(c => { c.AddPolicy("AllRequests", policy => { policy .AllowAnyOrigin() .AllowAnyMethod() .AllowAnyHeader(); }); }); #endregion services.AddControllers(); }
在其后的Configure函式中開啟中間件
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseRouting(); //開啟Cors跨域請求中間件 app.UseCors("AllRequests"); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); }
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/6424.html
標籤:.NET Core
