React+ASP.NET.Core : No 'Access-Control-Allow-Origin' header is present on the requested resource

前端 未结 3 1037
星月不相逢
星月不相逢 2020-12-29 08:17

Actually this is not a duplication post,I know a part of the title asked many times in stackoverflow community, I read all posts, and answers, but I think m

3条回答
  •  误落风尘
    2020-12-29 09:02

    With ASP.NET Core 2.1, when the controller throws an exception, which results in an error 500, the CORS headers are not sent. This should be fixed in the next version but until then you could use a middleware to fix this.

    see

    • https://github.com/aspnet/CORS/issues/90#issuecomment-348323102
    • https://github.com/aspnet/AspNetCore/issues/2378
    • https://github.com/aspnet/CORS/issues/46

    Also note that with credentials you need to explicitly add WithOrigins with host and port since most browsers simply ignore it otherwise (see https://stackoverflow.com/a/19744754/2477619):

      app.UseCors(builder =>
        builder
          .WithOrigins("http://localhost:4200")
          .AllowAnyHeader()
          .AllowAnyMethod()
          .AllowCredentials()
      );
    

提交回复
热议问题