web api,路由问题与我的帖子方法

时间:2017-01-10 16:49:09

标签: c# web-services angular asp.net-core-mvc asp.net-web-api-routing

我的网络api控制器路由存在问题。我已经使GET方法工作正常,但是现在,我尝试了一些POST并且路由似乎很糟糕。 我有一个前面和Angular2。在这里,您可以看到对webapi的调用:

 retour = this._http.post("http://localhost:5000/api/Particulier/add", _particulier, options)
        .map(postResult)
        .catch(this.handleError);

这是我的web api方法:

    [Route("api/[controller]/")]
public class ParticulierController : Controller
{

    [...]

    [HttpPost("add")]
    public async Task<bool> Add([FromBody]Particulier particulier)
    {
        var result = await _particulierService.Create(particulier);
        return result;
    }

    [...]

}

这很简单,但不起作用。 Chrome说我的网址不存在。我在同一个控制器中使用GET方法并且它们可以工作。

你可以帮帮我吗?

感谢,

2 个答案:

答案 0 :(得分:0)

方法Add([FromBody]Particulier particulier)可能与您的请求不匹配,因为缺少为Particulier参数指定的非可空属性。或者,这可能是反序列化请求的问题。

我建议使用Content-Type: text/json标题进行尝试,如果它不是缺少属性的问题。

retour = this._http({
        url: 'http://localhost:5000/api/Particulier/add',
        method: "POST",
        data: _particulier,
        headers: {'Content-Type': 'text/json'}
}).success(function(data, status, headers, config) {
    // map post result (data var)
});

答案 1 :(得分:0)

好的,它已经解决了。在Angular服务未被包含之前,不会执行该请求。

感谢所有人