How To Solve The CORS Or The Access-Control-Allow-Origin Header Error In .NET

In this article, we will see how we can quickly solve Access-Control-Allow-Origin Heade error or cors origin error in .NET.

I’m quite sure all developers, facing this issue once in their developer life, lol!. normally when we are working with APIs this issue can come if a request is from another origin and at that time our reaction is:

source: NBA Memes

To solve this issue we just need to enable CORS in an application. Here I’ll share one best way to solve Access-Control-Allow-Origin Header error.

Open web.config file and find “<system.webServer>” tag. Copy following code and paste inside that tag.

      <httpProtocol>
  <customHeaders>
    <add name="Access-Control-Allow-Origin" value="*" />
    <add name="Access-Control-Allow-Headers" value="Content-Type" />
    <add name="Access-Control-Allow-Methods" value="GET,POST,PUT,DELETE,OPTIONS" />
    <add name="Access-Control-Allow-Credentials" value="true" />
  </customHeaders>
</httpProtocol>

Your web.config will look like this.

And done, after applying this thing Access-Control-Allow-Origin Header error will be gone.

hope you guys found something useful. Please give your valuable feedback/comments/questions about this article. Please let me know how you like and understand this article and how I could improve it.

Submit a Comment

Your email address will not be published. Required fields are marked *

Subscribe

Select Categories