2

I am trying to send a request from an angular application and an ASP.NET web API both running in localhost on two different ports. When sending a request from the angular app(frontend) to the Web API I am getting a CORS error that "No 'Access-Control-Allow-Origin' header is present on the requested resource".

I tried a lot of ways to try to solve this issue like enabling cors in the WebApiConfig class, custom headers in the web.config and intercepting pre-flight checks but still, the issue occurred.

WebApiConfig.cs

var corsAttr = new EnableCorsAttribute("*", "*", "*");
config.EnableCors(corsAttr);

Web.config

<system.webServer>
    <httpProtocol>
    <customHeaders>
        <add name="Access-Control-Allow-Origin" value="*" />
        <add name="Access-Control-Allow-Headers" value="Content-Type, 
            Access-Control-Allow-Headers, Authorization, X-Requested- 
            With, X- AuthToken" />
        <add name="Access-Control-Allow-Methods" value="GET, POST, PUT, 
                 DELETE, OPTIONS" />
   </customHeaders>
   </httpProtocol>
   <modules runAllManagedModulesForAllRequests="true">
     <add name="HandleOptions" type="OptionsModule" />
   </modules>
 </system.webServer>
Sean goodlip
  • 29
  • 1
  • 5

2 Answers2

0

It seems that the site you requested doesn't allows any resources to be accessed from any other domain. Means the site you have requested has not implemented CORS (cross origin request sharing).

Temporary solution: You can use a proxy (https://crossorigin.me/https://google.com), all you need to do --> just append https://crossorigin.me/ before your url and for more details

Check HERE .

Second and permanent solution: That you have to implement cross origin resource sharing for the site you are requesting resources from.

Third one: 3rd is you can disable CORS from your browser (https://alfilatov.com/posts/run-chrome-without-cors/).

Check these sites for more details1,2

Avinash Singh
  • 4,970
  • 8
  • 20
  • 35
0

I see that you declared EnableCors in both WebApiConfig.cs and Web.config. This usually gives cors error saying something like:

'....origin 'www.example.com' blocked by cors policy due to multiple No 'Access-Control-Allow-Origin'('*','*').'

You should uncomment in either web-api.config or web.config and try.

krish
  • 237
  • 4
  • 14