带模板的CreatedAtRoute

时间:2019-12-30 12:05:33

标签: c# asp.net-core asp.net-core-webapi

我有一个问题。 如何正确创建此方法的CreatedAtRoute

HttpGet("exchangepoint")]
[ProducesResponseType(typeof(ExchangePointDto), 200)]
public async Task<IActionResult> GetExchangePoint(string name)

我尝试过这样:

result = CreatedAtRoute("GetExchangePoint",new { controller = "ShippingPlaceData", action = "GetExchangePoint", name = exchangePointToReturn.CustAccount }, exchangePointToReturn);

但这没用。

修改:
当我将CreatedAtRoute更改为CreatedAtAction时,它开始工作了,但这是正确的方法吗?

1 个答案:

答案 0 :(得分:1)

您可以使用CreatedAtRoute方法-在使用属性路由时,您需要像这样指定路由名称。

[HttpGet("exchangepoint", Name = "GetExchangePoint")]
[ProducesResponseType(typeof(ExchangePointDto), 200)]
public IActionResult GetExchangePoint(string name)
{

}

在post方法中,您可以像这样使用。

result = CreatedAtRoute("GetExchangePoint",new { controller = "ShippingPlaceData", action = "GetExchangePoint", name = exchangePointToReturn.CustAccount }, exchangePointToReturn);