0

I have a question. How to correctly create CreatedAtRoute to this Method:

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

I have tried like this:

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

But it didn't work.

Edit:
When I changed CreatedAtRoute to CreatedAtAction it started work but it's correct way to do this?

Badro Niaimi
  • 959
  • 1
  • 14
  • 28
Newer
  • 79
  • 1
  • 11
  • See if [this](https://www.csharpcodi.com/csharp-examples/Microsoft.AspNetCore.Mvc.ControllerBase.CreatedAtRoute(string,%20object,%20object)/) helps. – Rajesh G Dec 30 '19 at 12:32
  • @RajeshG after when i changed to CreatedAtAction it stated work but i'm not sure it is correct way. – Newer Dec 30 '19 at 12:38
  • see if [this](https://stackoverflow.com/questions/37839278/asp-net-core-rc2-web-api-post-when-to-use-create-createdataction-vs-created) helps you to decide. – Rajesh G Dec 30 '19 at 12:45
  • 1
    @Newer For `CreatedAtRoute` a *route name* is needed. The target action does not have a route name. ie: `[HttpGet("exchangepoint", Name = "GetExchangePoint")]` – Nkosi Dec 30 '19 at 12:47
  • @RajeshG thank, i will look on this – Newer Dec 30 '19 at 12:48

1 Answers1

1

You can use the CreatedAtRoute method - as you're using attribute routing, you need to specify the route name, like this.

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

}

And in the post method, you can use like this.

result = CreatedAtRoute("GetExchangePoint",new { controller = "ShippingPlaceData", action = "GetExchangePoint", name = exchangePointToReturn.CustAccount }, exchangePointToReturn);
Anuraj
  • 18,859
  • 7
  • 53
  • 79