【发布时间】:2018-08-07 17:33:55
【问题描述】:
在 EF Core 2 中,CreatedAtAction 将参数附加为 api/sample?id=1。如何配置它以返回 api/sample/1?
[ApiController]
[Authorize]
[Route("api/[controller]")]
public class MyController : ControllerBase
{
[HttpGet("{id}")]
public async Task<IActionResult> Get(int id)
{
var entity = await service.GetAsync(id);
if (entity == null)
return NotFound(entity);
return Ok(entity);
}
[HttpPut]
// Other codes are omitted for brevity
public async Task<IActionResult> Create([FromBody] Entity entity)
{
await service.AddAsync(entity);
await service.SaveAsync();
return CreatedAtAction(action, new { id = entity.Id }, entity);
}
}
【问题讨论】:
-
其实我已经想通了。我一直使用 nameOf(Create) 作为参数。它应该是 nameOf(Get)。谢谢!
标签: c# asp.net-core-2.0 asp.net-core-webapi