无法将对象从angular服务发布到web Api

无法将对象从angular服务发布到web Api,angular,asp.net-core-webapi,Angular,Asp.net Core Webapi,我是angular的新手,我在visual studio 2017中使用angular 6和asp.net core 2.0制作了一个应用程序。 我无法将对象从angular服务发布到web API。当我使用Ajax调用从HTML页面调用WebAPI时,它工作正常,但无法工作 当角度服务部门要求Post时。 我已经使用了所有可能的解决方案,比如更改HTTP头、在Web api参数中应用[FromBody]等等 下面是PostRequest的代码 createProduct(product): O

我是angular的新手,我在visual studio 2017中使用angular 6和asp.net core 2.0制作了一个应用程序。 我无法将对象从angular服务发布到web API。当我使用Ajax调用从HTML页面调用WebAPI时,它工作正常,但无法工作 当角度服务部门要求Post时。 我已经使用了所有可能的解决方案,比如更改HTTP头、在Web api参数中应用[FromBody]等等

下面是PostRequest的代码

createProduct(product): Observable<ProductModel> {
    const httpHeaders = this.httpUtils.getHTTPHeaders();  ///Format is     result.set('Content-Type', 'application/json')
     return this.http.post<ProductModel>('http://localhost:25875/api/Product/Index' , product, { headers: httpHeaders });
}

您正在使用带有默认HttpPost属性的属性路由,但在请求URL中调用操作名称

您需要更新操作以匹配所需的URL

[Route("api/[controller]")]
public class ProductController : Controller {
    //POST api/product/index
    [HttpPost("[action]")]
    public string Index([FromBody]ProductModel Product) {    
        Debug.WriteLine("Called Index");
        return "";
    }
} 
在没有路由模板的情况下使用HttpPost与调用

[HttpPost("")]

它将路由到POST api/产品,而不使用操作名称。这就是为什么在请求POST api/产品/索引时找不到404


Reference

404表示URL指向未找到的资源。您正在将请求发送到'http://localhost:25875/api/Product/Index'但该方法在控制器中的路径为'http://localhost:25875/api/Product如果我使用ajax从我的html页面调用相同的url,它会被点击,为什么不从angular服务的post请求中点击呢@托莱多。我试着改变你所说的,但仍然是同一个问题。你能为有故障和正常工作的请求添加网络选项卡的输出吗?
[HttpPost("")]
[HttpPost]
[Route("")]