Quantcast
Channel: Web API
Viewing all articles
Browse latest Browse all 4850

How to enable Cors in ASP.NET Web Api

$
0
0

Hello,

I'm learning Angular 2 and web api core at the same time.  Both applications are running in my local machine. Angular 2 is in VS Code and the web api is in Visual Studio. 

Requesting data from the web api is working just fine in Micosoft Edge, but I'm getting the following error inChrome and FireFox:

XMLHttpRequest cannot load http://localhost:65400/api/expenses. 
No 'Access-Control-Allow-Origin' header is present on the requested resource.
Origin 'http://localhost:3000' is therefore not allowed

I've tried everything as explained in this article, but no luck so far. Here's my code:

services.AddCors(options =>
{
    options.AddPolicy("AllowAllMethods",
           builder =>
           {
              builder.WithOrigins("http://localhost")
                     .AllowAnyHeader();
           });
});

Then I've tried this:

[HttpGet()]
[EnableCors("AllowSpecificOrigin")]
public IActionResult GetExpenses()
{
   return Ok(_expRepo.GetExpenses());
}

It didn't work. Then I tried this as well

app.UseCors("AllowSpecificOrigin");

It still didn't work, i.e. I'm still getting the same error.

Thanks for helping


Viewing all articles
Browse latest Browse all 4850

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>