C# 如何返回createdatroute位置?
我正在尝试创建这样一个记录:C# 如何返回createdatroute位置?,c#,asp.net,.net,asp.net-web-api,C#,Asp.net,.net,Asp.net Web Api,我正在尝试创建这样一个记录: [Route("", Name = "CreateAccount")] [HttpPost] public async Task<IHttpActionResult> CreateAccount([FromBody] JObject account) { var response = await _accountService.Create(account); return CreatedAtRoute( routeN
[Route("", Name = "CreateAccount")]
[HttpPost]
public async Task<IHttpActionResult> CreateAccount([FromBody] JObject account)
{
var response = await _accountService.Create(account);
return CreatedAtRoute(
routeName: "GetAccount",????);
}
正如您所见,控制器不接受任何参数,而是从Request.RequestUri.PathAndQuery
创建记录后,如何返回有效的
CreatedAtRoute
结果?您可以尝试的一件事是将路由值作为对象传递。框架应该将它们(路由值)转换为查询字符串
[HttpPost]
[Route("", Name = "CreateAccount")]
public async Task<IHttpActionResult> CreateAccount([FromBody] JObject account) {
var response = await _accountService.Create(account);
var model = await response.Content.ReadAsAsync<MyModel>();
return CreatedAtRoute(
routeName: "GetAccount",
routeValues: new { id = model.Id, name = model.Name }, // can include other properties
content: model);
}
[HttpPost]
[Route("", Name = "CreateAccount")]
public async Task<IHttpActionResult> CreateAccount([FromBody] JObject account) {
var response = await _accountService.Create(account);
var model = await response.Content.ReadAsAsync<MyModel>();
return CreatedAtRoute(
routeName: "GetAccount",
routeValues: new { id = model.Id, name = model.Name }, // can include other properties
content: model);
}
LOCATION: http://localhost/api/accounts?id=5&name=JDoe