Search code examples
asp.net-web-apiiis-10

How to access web API on client machine


I have hosted web API on Windows server 2012 . I set the binding like port and ipaddress.

After configuration, I browse the API and it's working fine.

Now I wanted to access my configured API from other machines.

What configuration I need to do in my web config.

I am beginner on deployment stuff. Please help me out on this.

Thanks in advance.


Solution

  • You may need to set Access-Control-Allow-Origin headers. Specifically for JSON:

    [AllowCrossSiteJson]
    public ActionResult YourMethod()
    {
        return Json("Works better?");
    }
    

    Or for a whole controller:

    [AllowCrossSiteJson]
    public class ValuesController : ApiController
    {
    

    You can also edit your web.config to include:

    <httpProtocol>
       <customHeaders>
          <clear />
          <add name="Access-Control-Allow-Origin" value="*" />
       </customHeaders>
    </httpProtocol>
    

    Source: https://learn.microsoft.com/en-us/aspnet/core/security/cors