asp.net Cross domain instance of core webapi server configuration

Time:2020-7-15

In the separate development of front end and back end, the server only provides API interface for the front end, and the front and back end are often deployed separately, so the browser cross domain problem will appear. asp.net Core provides a simple and elegant solution.

Add the following code to configure in the startup file (replace“ http://localhost : 8080 “is your front-end deployment address, and the front-end address tested here is the local 8080 port)

Note: asp.net For core 2.0, nuget package is required for the following: Microsoft.AspNetCore.Cors


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

If you are in a development environment, you only need to replace it builder.WithOrigins (” http://localhost : 8080 “) builder.AllowAnyOrigins () to allow cross domain access to any source address (not recommended for production environments)

The above article asp.net The example of cross domain configuration of core webapi server is the whole content shared by Xiaobian. I hope it can give you a reference, and also hope that you can support developeppaer more.