Stack Exchange
ASP.NET Core Web API POST No 'Access-Control-Allow-Origin' header is present
I have a GET Web API method in another controller working fine with UseCors allowing any origin enabled in Startup.cs
When I try to call a POST method I get a No 'Access-Control-Allow-Origin' header is present error returned along with a 500 error.
What am I doing wrong?
using System.Collections.Generic;
using CrossSell.Business.Exceptions;
using CrossSell.Business.Interfaces;
using CrossSell.Entities;
using Microsoft.AspNetCore.Mvc;

namespace CrossSell.API.Controllers
{
    [Produces("application/json")]
    [Route("api/Product")]
    public class ProductController : Controller
    {
        private readonly IProductManager productManager;

        public ProductController(IProductManager productManager)
        {
            this.productManager = productManager;
        }

        // POST: api/Product
        [HttpPost]
        public IEnumerable<Opportunity> Post([FromBody]ClientIdentifiable[] clients)
        {
            try
            {
                return productManager.GetCrossSellOpportunities(clients);
            }
            catch (NoInForceOrHistoricalPoliciesException)
            {
                return new[] { new Opportunity(true, "No In Force or historical policies") };
            }
        }

    }
}

I'm calling the Post method from my React app (which runs on localhost:3000):
  getDashboardData() {
    var self = this;
    axios.post( 'http://localhost:5000/product/api/', this.state.clientIdentifiables).then(function (response) {           
           console.log(response);
        });
  }


Could you try to debug the error? And you make POST /product/ instead of api/product/.

Thanks corrected the api/product. It is calling that path.

Funnily enough, @IlyaChumakov I couldn't answer your debug the error question so I started to debug and found the problem.

I forgot to add the dependency injection entry in ConfigureServices method of Startup.cs
public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();
            services.AddTransient<IClientManager, ClientManager>();
            services.AddTransient<IClientRepository, ClientRepository>();

            // added the following and it hit the web api method correctly
            services.AddTransient<IProductManager, ProductManager>();
            services.AddTransient<IProductRepository, ProductRepository>();
        }

