How to set access control allow origin on particular file web.config

FlavorScape picture FlavorScape · Oct 25, 2012 · Viewed 50.6k times · Source

If this were a dynamic response, I'd simply do Response.Headers.Add("Access-Control-Allow-Origin", "*"); but I have a static file I'd like to allow cross domain access to.

Is there a way to assign this header to a particular file just using web.config? Say it's just example.com/flat.json

I guess I could route the file to dynamic page, but that would be a bit silly.

Answer

Tariqulazam picture Tariqulazam · Oct 25, 2012

This should work

<location path="Sample.txt">
    <system.webServer>
      <httpProtocol>
        <customHeaders>
          <add name="Access-Control-Allow-Origin" value="*" />
        </customHeaders>
      </httpProtocol>
    </system.webServer>
  </location>