Asp.net 如何在asp net核心api中使用Created(或CreateDataAction/CreatedAtRoute)
我想将我的api告知消费者新创建对象的位置。我知道有CreatedAtRoute和CreateDataAction,但我不确定如何使用它 以下是我尝试过的: 我想指出一个Get资源。它将ID作为输入:Asp.net 如何在asp net核心api中使用Created(或CreateDataAction/CreatedAtRoute),asp.net,asp.net-core,Asp.net,Asp.net Core,我想将我的api告知消费者新创建对象的位置。我知道有CreatedAtRoute和CreateDataAction,但我不确定如何使用它 以下是我尝试过的: 我想指出一个Get资源。它将ID作为输入: [HttpGet("/${id}", Name = "GetProduct")] [ProducesResponseType(typeof(Produkt), 200)] public IActionResult Get([FromRoute] int id) {
[HttpGet("/${id}", Name = "GetProduct")]
[ProducesResponseType(typeof(Produkt), 200)]
public IActionResult Get([FromRoute] int id)
{
// some code...
return Ok(...);
}
当通过我的发布路线创建产品时,我希望通过位置标题指向此资源:
尝试1
这将返回一个位置标题:$15003
尝试2
这个函数可以工作并返回/api/v1.0/produkte/16004,但似乎不需要使用当前请求来指向新位置。另外,我不确定这是否是一种好的做法?在Get方法的路径中,同时取出前导/和$out,即它应该是{id}。有引导/进入意味着路线将相对于应用程序的基础;取出它会使方法的路由改为相对于控制器的基本路径。$在路由中被视为一个文字字符,因此它在尝试1时出现在位置标头中。一旦您进行了更改,您应该会发现您的CreatedAtRoute调用工作正常。引用: 201创建的状态代码表示请求已被删除 已完成,并已导致创建一个或多个新资源。 由请求创建的主资源由 响应中的位置标题字段,或者,如果没有位置字段 通过有效的请求URI接收 标识资源的内容取决于上下文。在我的解释中,如果创建的资源位于,那么标识符可以是。CreateDataAction在我看来提供了最好的输出。以下控制器代码将满足您的需要:
[Route("api/products")]
[ApiController]
public class ProductsController : ControllerBase
{
private readonly IProductRepository productRepository;
public ProductsController(IProductRepository productRepository)
{
this.productRepository = productRepository;
}
[HttpPost]
[Route("")]
[ProducesResponseType(StatusCodes.Status201Created)]
public ActionResult<Product> CreateProduct(ProductCreateDto product)
{
if (product is null)
return BadRequest(new ArgumentNullException());
var entity = productRepository.CreateProduct(product);
return CreatedAtAction(nameof(GetProduct), new { id = entity.ID }, entity);
}
[HttpGet]
[Route("{id}")]
public ActionResult<Product> GetProduct(int id)
{
return productRepository.GetProduct(id);
}
}
将产生以下响应:
HTTP/1.1 201 Created
Date: Mon, 12 Oct 2020 09:50:00 GMT
Content-Type: application/json; charset=utf-8
Server: Kestrel
Content-Length: 29
Location: http://localhost:5000/api/products/1
{"id":1,"name":"ACME Widget"}
我还要补充一点,CreateNewProduct方法的ProducesResponseType注释应该指定201返回码,而不是200。谢谢Graham!我太专注于CreatedAt方法了,以至于在Get方法中犯了一个愚蠢的错误:拥有一个伟大的2018尝试2是一个伟大的解决方案。聪明的举动。
[Route("api/products")]
[ApiController]
public class ProductsController : ControllerBase
{
private readonly IProductRepository productRepository;
public ProductsController(IProductRepository productRepository)
{
this.productRepository = productRepository;
}
[HttpPost]
[Route("")]
[ProducesResponseType(StatusCodes.Status201Created)]
public ActionResult<Product> CreateProduct(ProductCreateDto product)
{
if (product is null)
return BadRequest(new ArgumentNullException());
var entity = productRepository.CreateProduct(product);
return CreatedAtAction(nameof(GetProduct), new { id = entity.ID }, entity);
}
[HttpGet]
[Route("{id}")]
public ActionResult<Product> GetProduct(int id)
{
return productRepository.GetProduct(id);
}
}
POST http://localhost:5000/api/products HTTP/1.1
Host: localhost:5000
Connection: keep-alive
Content-Length: 25
Content-Type: application/json
{ "name": "ACME Widget" }
HTTP/1.1 201 Created
Date: Mon, 12 Oct 2020 09:50:00 GMT
Content-Type: application/json; charset=utf-8
Server: Kestrel
Content-Length: 29
Location: http://localhost:5000/api/products/1
{"id":1,"name":"ACME Widget"}