No 'Access-Control-Allow-Origin' header is present on the requested resource

Most of us will saw this error message in web browser console while working with ASP.NET WEB API. The reason behind this error is CORS. That means Cross Origin Resource Sharing. When we try to access a web service from our application, if the application domain and the web service domain is different, then this problem will be occurs.

For example:
Suppose our application is running on www.examplesite.com
from where we are trying to access the web service which domain address is www.exampleservice.com/api/employee. Since the domain address is different, it through the error.


What we can do?

In our Web API Application:
1. Go to Tools
2. Nuget Package Manager
3. Package Manager Console
4. Write install-package Microsoft.AspNet.WebApi.Cors
5. Now go to WebApiConfig.cs file
6. Paste the following lines:

EnableCorsAttribute cors = new EnableCorsAttribute("*""*""*");
config.EnableCors(cors);


and you are DONE!

Comments

Popular posts from this blog

Top 5 AI Tools Every Developer Should Use to Boost Coding Efficiency

How To Generate Random Dates Between Two Date Range in SQL Server

Find the number of columns in a table