How to Solve .Net 5 webapi cross domain Issue

Recently, I want to write a web app, and then I think of the cross-domain problem. I think of the last time I studied it for a long time before I succeeded. This time, I’d better record it to prevent spending a lot of time looking for methods in the future.

There are several solutions to cross-domain problems:

1. Jsonp requests web API through browser tag; Avoid cross-domain issues
2. Simulate HTTP requests in the background to request webapi
3. Allow cross-domain on the server-side.

I haven’t tried the first method. The second method initiates a request through the back-end language. Naturally, there is no cross-domain problem, but it doesn’t meet the requirements of my project. Then there is the third method. Use cross to enable web API projects to support requests for different domain names. I have also studied it when using the. Net framework before, and follow it   The method of this article runs successfully.

.Net core also provides the corresponding package Microsoft.aspnetcore.cores. After installing the package, we found the configure services method in startup.cs and registered cros in it.

Then use the registered service in the configure method.

In this way, the cross domain problem can be solved. However, it should be noted that the location of app. Usecors needs to be placed behind userouting() and before useauthorization(). When I first use it, usecors is placed at the top, resulting in unsuccessful attempts. It takes me a lot of time. I hope I can remember it next time.

Similar Posts: