2017-05-01 13 views
0

私は ASP.NETコアのWeb API POSTいいえ「アクセス制御 - 許可 - 起源」ヘッダーは

ときStartup.cs

で有効に任意の起源を可能UseCorsで細かい作業を別のコントローラでGETウェブAPIメソッドを持っている存在でありますPOSTメソッドを呼び出そうとしましたが、No 'Access-Control-Allow-Origin'ヘッダーが500エラーとともに返されました。

私は間違っていますか?

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") }; 
      } 
     } 

    } 
} 

私は(ローカルホスト上で実行されます:3000):アプリに反応からPostメソッドを呼んでいる

getDashboardData() { 
    var self = this; 
    axios.post('http://localhost:5000/product/api/', this.state.clientIdentifiables).then(function (response) {   
      console.log(response); 
     }); 
    } 
+0

エラーをデバッグできませんか。そしてあなたは 'api/product /'の代わりに '/ product /'をPOSTします。 –

+0

api/productを修正していただきありがとうございます。その道を呼んでいます。 – ParleParle

+2

Funnily enough、@IlyaChumakov @IlyaChumakov私はあなたのデバッグにエラーの質問に答えることができませんでしたので、私はデバッグを開始し、問題を発見しました。 – ParleParle

答えて

1

私はStartup.cs

のConfigureServices方法で依存性の注入エントリを追加するのを忘れ
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>(); 
     } 
関連する問題