One of the most common issues we face is creating a REST API for our client which is hosted on a brand new domain while the client application is being hosted on the clients old domain. Specifically when client demands that we carry both the system parallel for couple of months until he/she has confidence on the upgraded API's. In our own company we found this issue when launching a brand new domain for a new application. Unfortunately the new application hosted on the new domain talks to our old REST API hosted on our company domain. Problem!
Yes you have that ugly JSON-P option but being a thorough bred programmer I am lazy. So how do we deal with these kind of problems.
Microsoft comes to our rescue, we can do it in 3 steps without manually messing with web.config.
Head over to Visual Studio 2015 open your WebAPI project which runs the REST API.
Open Package Manager Console[Tools->NuGet Package Manager->Package Manager Cosole]
Run this following command Install-package Microsoft.AspNet.WebApi.Cors
That's it now you just need to tell the WebAPI engine which controller needs to be opened for your domain(cross-origin)
In your solution manager find a file called WebApiConfig.cs, in this file you will find the following code. Add the code that is highlighted
On top of the Controller you want to expose, put this attribute
The url of your domain should not end with a backslash.
That's it and you are ready to call your web API from the domain of your choice.