how to return createdatroute location?

Viewed 2559

I'm attempting to create a record like so:

[Route("", Name = "CreateAccount")]
[HttpPost]
public async Task<IHttpActionResult> CreateAccount([FromBody] JObject account)
{

    var response = await _accountService.Create(account);

    return CreatedAtRoute(
        routeName: "GetAccount",????);
}

The creation of the record succeeds; however. I'm not sure how to return a CreatedAtRoute object?

This is my GET controller method:

[Route("", Name = "GetAccount")]
[HttpGet]
public async Task<IHttpActionResult> GetAccount()
{
    var query = Request.RequestUri.PathAndQuery.Split('/')[2];
    var response = _accountService.Get(query);

    if (!response.Result.IsSuccessStatusCode)
    {
        return NotFound();
    }

    var readAsAsync = response.Result.Content.ReadAsAsync<object>();
    if (readAsAsync == null)
    {
        return NotFound();
    }

    var result = await readAsAsync;
    return Ok(result);
}

as you can see the controller does not accept any parameters, rather we get the parameters from Request.RequestUri.PathAndQuery

After creating a record, how do we return a valid CreatedAtRoute result?

1 Answers
Related