Search code examples
c#.netasp.net-web-api

What is the difference between returning CreatedAtAction() and RedirectToAction in ASP.NET Web API?


I am wondering what is the difference between CreatedAtAction and RedirectToAction in ASP.NET Web API ?

When to return CreatedAtAction, and when to return RedirectToAction?

I got the same response in Swagger when I used both of them


Solution

  • Key Differences

    Status Code:

    Purpose:

    • CreatedAtAction: This is used to represent a resource creation. In addition, it includes the location of the created new resource in the response.
    • RedirectToAction: This is used to inform the client that has been redirected to some other action.

    Response:

    • Swagger might have similar responses for these two as Swagger typically demonstrates the body in the response. If the type of data that an action redirects to or creates is of the same type of data, the responses may look the same.
    • The status codes and the values of the Location header are different, though, which would be apparent in the complete HTTP response.