简体   繁体   中英

web api, routing issue with my post method

I have an issue with my web api controler routing. I've made GET methods that works fine, but now, I try some POST and routing seems to be bad. I've have a front and with Angular2. Here, you can see the call to webapi :

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

Here it's my web api method :

    [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;
    }

    [...]

}

it's simple, but doesn't work. Chrome say me that the url doesn't exist. I have GET methods in the same controller and they works.

can you help me ?

thanks,

The method Add([FromBody]Particulier particulier) may not be matching your request because there are missing non-nullable properties specified for the Particulier parameter. Or, it could be an issue with deserializing the request.

I recommend trying it with the Content-Type: text/json header if it's not an issue with missing properties.

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

Ok, it's resolved. The request is not executed until the Angular service is not subsribed.

thanks to everyone

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM