What is CORS Enable ? How to use it in ASP.NET MVC Core ?

Rajesh Pillai
2 min readJun 21, 2019

--

CORS Stands for Cross-Origin Resource Sharing. It is a W3C Standard that allows a server to relax the same-policy. Using CORS, a server can explicitly allow some cross-origin requests while rejecting others.

Let’s get started with creating two projects one called webservice which hosts a Web api controller in ASP.NET MVC Core and other one called webclient in Angular 7. This two projects are hosted in different server. Lets make a HttpRequest from WebClient to WebService like in below picture .

After the HttpRequest we will get an error stated that your webclient origin has been blocked by CORS policy and No Access-Control-Allow-Origin.

To resolve this cors error we need to put some line of code inside the startup.cs file in ConfigureServices and in Configure methods. Like in the below picture.

After adding this lines of code in start.cs file we can make HttpRequest from any origin or if we want to pass HttpRequest to particular origin then this also possible by CORS. In below picture we can see that we have added two CorsPolicy for their respected origins.

In case your basics are still unclear suggesting to play and go through with following ASP.NET MVC core interesting video: -

--

--

Rajesh Pillai

I’m a Programmer and learner. Enthusiastic to learn new things and upgrade myself. I write blogs on what i am learning and i feel to share with you guys.