Pages

Thursday, May 17, 2018

ASP.NET Core 2.1 : ActionResult<TValue&gt

ASP.NET Core 2.1 is almost released, we now have access to ASP.NET Core 2.1 RC1. Among other nice features like SignalR (Yes, SignalR is included in ASP.NET Core 2.1) which is getting shipped with ASP.NET Core 2.1, one of my favorites is the introduction of ActionResult<TValue>.

So this is very simple, and we are searching for an Employee by Id and returning an Employee. Having Employee is the return type here is very useful for things like documenting the API using Swagger for instance.

But this looks like a bad code, what if there is no Employee by given Id. In that we don’t usually return null, we return HTTP Status Code: NotFound.

But if we are to return that, we no longer can return an Employee type, we are returning an IActionResult. And the code changes to following.