locked
How to enable Cors in ASP.NET Web Api RRS feed

  • Question

  • User-1460211239 posted

    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 in Chrome 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

    Sunday, December 18, 2016 1:15 AM

Answers

  • User1751268424 posted

    Hi,

    Try using port number, then your code should be like this:

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

    Try the above first, then if not work, change these codes:

    [EnableCors("*", "*", "*")]
    public class YourApiController
    {
       
    }

    Have fun


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, December 18, 2016 3:52 AM
  • User-2057865890 posted

    Hi Crhama,

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

    The Origin consists of the Scheme, Host, and the Port number.

    builder.WithOrigins("http://localhost:65400")

    The current versions of all major browsers include support for CORS.

    IE10+ does not make a CORS request if port is the only difference (IE Bug #781303)

    Best Regards,

    Chris

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, December 20, 2016 5:58 AM

All replies

  • User1751268424 posted

    Hi,

    Try using port number, then your code should be like this:

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

    Try the above first, then if not work, change these codes:

    [EnableCors("*", "*", "*")]
    public class YourApiController
    {
       
    }

    Have fun


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, December 18, 2016 3:52 AM
  • User-2057865890 posted

    Hi Crhama,

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

    The Origin consists of the Scheme, Host, and the Port number.

    builder.WithOrigins("http://localhost:65400")

    The current versions of all major browsers include support for CORS.

    IE10+ does not make a CORS request if port is the only difference (IE Bug #781303)

    Best Regards,

    Chris

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, December 20, 2016 5:58 AM