Solve cors problem in portal azure

1

I have an API to consume an uploaded service to my azure portal, but I have the following problems:

  •   

    OPTIONS [ API path ] 404 (Not Found)

  •   

    XMLHttpRequest can not load [ API Path ].
      Response to preflight request does not pass access control check: No   'Access-Control-Allow-Origin' header is present on the requested   resource. Origin ' API Path ' is therefore not allowed   access. The response had HTTP status code 404.

I would like to know if it is possible to configure my virtual machine to fix this problem, or I should add the web.config file to the corresponding configuration and upload my API to the portal again

    
asked by Pedro Miguel Pimienta Morales 04.04.2017 в 18:50
source

2 answers

1

It's a 404 It means that it does not exist Check that you have everything well configured in azure and in your WC You can put the webconfig configuration node

    
answered by 04.04.2017 в 18:57
1

There are a couple of things you can change to solve your problem.

1 - In your controller add an Options method and only return an OK (if you are in previous versions of web api use HttpResponseMessage instead of IHttpActionResult)

    [HttpOptions]
    public IHttpActionResult Options() {
        return Ok();
    }

This is necessary so that the client knows that he needs your API service to share his resources.

2 - you need to make a couple of changes in your web.config. Within the System.WebServer Node add or replace the following:

    <httpProtocol>
      <customHeaders>
        <add name="Access-Control-Allow-Origin" value="*" />
        <add name="Access-Control-Allow-Headers" value="Content-Type, Authorization, Origin, X-Requested-With, Accept, Cache-Control" />
        <add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
      </customHeaders>
    </httpProtocol>
    <handlers>
      <remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
      <remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
      <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
      <add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
      <add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
      <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
    </handlers>

I hope you find it useful.

Cordial greetings.

    
answered by 06.04.2017 в 08:19