Blazor 請求被 CORS 策略阻止
我正在嘗試從
Blazor(客戶端)客戶端向伺服器發送請求,但我不斷收到此錯誤:訪問 '
$$ route $$’(重定向自’$$ other route $$’) 從原產地’$$ origin route $$’ 已被 CORS 策略阻止:請求的資源上不存在“Access-Control-Allow-Origin”標頭。如果不透明的響應滿足您的需求,請將請求的模式設置為“no-cors”以獲取禁用 CORS 的資源。
在伺服器上,我已經
CORS在管道中添加了擴展,但無濟於事:伺服器啟動
public void ConfigureServices(IServiceCollection services) { services.AddCors(); services.AddResponseCompression(options => { options.MimeTypes = ResponseCompressionDefaults.MimeTypes.Concat(new[] { MediaTypeNames.Application.Octet, WasmMediaTypeNames.Application.Wasm, }); }); } public void Configure(IApplicationBuilder app, IHostingEnvironment env) { app.UseCors(x => x.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin().AllowCredentials()); app.UseResponseCompression(); app.UseMvc(); app.UseBlazor<Client.Startup>(); }Blazor 客戶端請求
public async Task<Catalog> GetCatalogAsync() { try { HttpRequestMessage message = new HttpRequestMessage { RequestUri = new Uri(BASE_PATH + Routes.GET_CATALOG), //BASE_PATH= 172.XX.XX.XX:8600 Method = HttpMethod.Get }; var resp = await this.client.SendAsync(message); // client is HttpClient var resultString = await resp.Content.ReadAsStringAsync(); var result = JsonConvert.DeserializeObject<Catalog>(resultString); return data; } catch (Exception ex) { throw; } }控制器
[HttpGet] [Route(Routes.GET_CATALOG)] public async Task<Catalog> GetCatalogAsync() { try { var registry = await this.adminService.GetCatalogAsync(); return registry; } catch (Exception ex) { throw; } }少量
[Serializeable] public struct Catalog{ }我還能做些什麼才能訪問我的伺服器?是因為 Blazor 嗎?如您所見,我已經添加了
UseCors(...).PS
我已經與客戶端一起發布了我的Blazor Server項目。它們在同一個目錄中。這個文件夾我把它放在電腦上,我正在嘗試從我的電腦打開blazor:
172.168.18.22:8600/更新
我也嘗試將標題添加到我
HttpRequestMessage的無濟於事:HttpRequestMessage message = new HttpRequestMessage { RequestUri = new Uri(BASE_PATH + Routes.GET_CATALOG), Method = HttpMethod.Get, }; message.Headers.Add("Access-Control-Allow-Origin","*"); message.Headers.Add("Access-Control-Allow-Credentials", "true"); message.Headers.Add("Access-Control-Allow-Headers", "Access-Control-Allow-Origin,Content-Type");
不知何故,問題是由於記憶體在瀏覽器上的非常舊的客戶端版本造成的。在此問題之後,我再也不會忘記清除瀏覽器記憶體。謝謝大家的幫助和支持!
@Bercovici Adrian,您為什麼要在您的應用程序中添加 CORS 支持?您是否提出跨源請求?如果您不這樣做,請不要嘗試通過添加可能導致更細微錯誤的不必要配置來解決問題。
像往常一樣,沒有看到這個應用程序的回購,不能進一步幫助你。
更新:
這是什麼UseBlazor?
您應該將您的應用程序升級到最新版本…
新更新:
抱歉,我使用的是 Blazor 的目前預覽版
創業班
public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } // 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) { services.AddMvc().AddNewtonsoftJson(); services.AddResponseCompression(opts => { opts.MimeTypes = ResponseCompressionDefaults.MimeTypes.Concat( new[] { "application/octet-stream" }); }); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { app.UseResponseCompression(); if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); app.UseBlazorDebugging(); } **// Instead of UseBlazor** app.UseClientSideBlazorFiles<Client.Startup>(); app.UseStaticFiles(); app.UseRouting(); **// This configure your end points** app.UseEndpoints(endpoints => { endpoints.MapDefaultControllerRoute(); endpoints.MapFallbackToClientSideBlazor<Client.Startup>("index.html"); }); } }請注意,我已經刪除了 CORS 的配置,因為您的客戶端和伺服器共享同一個域。請使用文件如何正確配置 CORS。
試試這個,看看它是否適合你(我猜你的問題與端點的配置有關。不知何故,在我看來,因為你沒有配置端點,你的請求被重定向,因此你得到了消息由您在上面顯示。)
接下來要做的是檢查您的 http 請求是否經過適當處理。但首先檢查端點。