This issue is coming from WEB API application. When we created a WEB API project and created some methods to get or post data. But problem occurs when you try to access the api from another domain and try to get data or post data to WEB API application. At this time you get this error “No ‘Access-Control-Allow-Origin’ header is present on the requested resource. If an opaque response serves your needs, set the request’s mode to ‘no-cors’ to fetch the resource with CORS disabled.

We can resolve this error by 2 ways.

1) By using System.Web.Http.Cors;

In this method you have to use two namespaces given below.

using System.Net.Http.Headers;
using System.Web.Http.Cors;

After that go to WebApiConfig file which you will find in App_Start folder on root in your application. After that copy paste below line into your Register method and change your url from which you are hitting your WEB API project. First parameter is your URL , second parameter is your header information and third parameter is your methods. So if you provide * then it will allow all the requests coming from the url with all header information. You can also limit it to by providing your specific urls, headers and methods.

config.EnableCors(new EnableCorsAttribute(” YOUR URL “, “*“,”*“));

2) By using customHeaders in Web.config.

In this method you have to go to your Web.config file of your WEB API project and find the system.webServer tag. Now paste the below lines of code inside this tag as shown in the image.

Access-Control-Allow-Origin Header-web-config
Access-Control-Allow-Origin Header-web-config

After using any of these methods kindly clean your solution and build it. Now its ready to respond your another project.

Happy Coding. 🤓🤓 💻 💻 

For other useful video kindly go to my youtube channel.

Manoj Deshwal

Leave a Comment