asp. net core webapi server side configuration cross domain instance

  • 2021-10-15 10:23:50
  • OfStack

In the development of front-end separation, the server only provides api interface for the front-end, and the front-end is often deployed separately, so the browser cross-domain problem will occur. asp. net core provides a simple and elegant solution.

Add the following code to Configure in the startup file (replace "http://localhost: 8080" for your front-end deployment address, where the front-end address tested is local port 8080)

Note: asp. net core 2.0: Microsoft. AspNetCore. Cors


app.UseCors(builder =>
   {
    builder.AllowAnyHeader();
    builder.AllowAnyMethod();
    builder.WithOrigins("http://localhost:8080");
   });

If you simply replace builder. WithOrigins ("http://localhost: 8080") with builder. AllowAnyOrigins () in the development environment, you can allow cross-domain access to addresses from any source (not recommended for production)


Related articles: