Notes

Add CORS Header to an ASP.NET MVC project

Edit on GitHub

System Administration

Add the following to Web.config for the ASP.NET MVC app (inside the <configuration> block)

 1<!-- START: Add CORS header for WordPress site so we can dynamically fetch popular users there -->
 2<configuration>
 3  <system.webServer>
 4    <httpProtocol>
 5      <customHeaders>
 6        <add name="Access-Control-Allow-Origin" value="https://other.mydomain.net" />
 7      </customHeaders>
 8    </httpProtocol>
 9  </system.webServer>
10</configuration>
11<!-- END: Add CORS header for WordPress site so we can dynamically fetch popular users there -->

Make sure a <configuration>, <system.webServer>, <httpProtocol>, or <customHeaders> block doesn’t exist already. Better to search for these first and then add the code accordingly.

Save the Web.config file and restart the site from IIS. You can now make a call from https://other.mydomain.net to https://main.mydomain.net/ using JavaScript fetch. For example:

1let endpoint = 'https://main.mydomain.net/Snapshot/landing-grid-mobile?count=12'
2
3async function getPopularUsers(url) {
4  const response = await fetch(url)
5  return await response.json()
6}
7
8getPopularUsers(endpoint).then(data => console.info(data))