SHARE:

ASP.NET Core 5 Route to Code: Taking advantage of Microsoft.AspNetCore.Http json extensions

Introduction

A while ago, I have introduced a cool feature of ASP.NET Core: how to build a lightweight API without using a framework. I named it like this: Nano service with ASP.NET Core: which you can find here: https://anthonygiretti.com/2020/06/29/nano-services-with-asp-net-core-or-how -to-build-a-light-api, I still like this feature and Microsoft called it: Route to Code, how could I not have thought about it before?
In this article I will tell you about an improvement made by ASP.NET Core 5: a simpler writing of Route to Code services thanks to JSON extensions for HttpRequest and HttpResponse based on System.Text.Json brought by ASP. NET Core 3 in 2019.

How to play with HttpRequest and HttpResponse before and with ASP.NET Core 5

Let’s consider two scenarii: implementing GET & POST endpoints with ASP. NET Core 3 with its new JSON library System.Text.Json. The GET endpoint will return a list of countries and the POST endpoint will create a country. In both cases we need to deal with JSON data, with GET want to serialize the list of country in the HttpResponse and in the POST endpoint we want to deserialize the JSON data from the HttpRequest:

As you can see the implementation is very simple, you just have to call the classical JSON serialization / deserialization methods aka JsonSerializer.Serialize() and JsonSerializer.Deserialize<T>(). Note that you also need to read the request body with a StreamReader to convert it into text like this:

using (StreamReader reader = new StreamReader(context.Request.Body))
{
   bodyText = await reader.ReadToEndAsync();
}

What if I tell you with ASP.NET Core 5 this job is very simpler ? 😉

ASP.NET Core 5 includes a built-in assembly that contains new JSON extensions based on System.Text.Json for HttpRequest and HttpResponse. That means you don’t need to download any Nuget package, it’s a part of ASP.NET Core:

So now how can we rewrite our Route to Code services ? like this:

As you can see, the code is now much simpler. Another great feature: HasJsonContentType() extension on the HttpRequest object. This method allows to know if the content received is in JSON or not, and you are now able to handle it, for example, you can return the status HttpStatusCode.UnsupportedMediaType if you don’t received an expected JSON payload is you request body. Practical isn’t it ? 🙂

Conclusion

Route to Code is a great feature of ASP.NET Core. This feature is evolving in ASP.NET Core 5 and it’s great. I strongly suggest you to learn more about this feature I’m sure you’ll like too. If you are interested you can watch this interesting video on Channel 9:

Enjoy 🙂

Written by

anthonygiretti

Anthony is a specialist in Web technologies (14 years of experience), in particular Microsoft .NET and learns the Cloud Azure platform. He has received twice the Microsoft MVP award and he is also certified Microsoft MCSD and Azure Fundamentals.
%d bloggers like this: