首页 > 解决方案 > CreatedAtRoute 与模板

问题描述

我有个问题。如何正确创建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);

但它没有用。

编辑:
当我更改CreatedAtRouteCreatedAtAction它开始工作但这是正确的方法吗?

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

解决方案


您可以使用该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);

推荐阅读