One criticism about Asp.Net web api is the ceremony to create a simple web api. This criticism has some reason: when you create a bare-bones web api with this command:
You will get something like this:
As you can see, there is a lot of plumbing going on in Program.cs and Startup.cs and the real meat is in Controllers\WeatherForecastController.cs:
Even that has a lot going on: the ApiController, Route and HttpGet attributes, besides the code (well, that’s needed in any platform, anyway ). For comparison, we could create the same web api using node.js with Express with this code:
As you can see, there is a lot less code for the same api. The Express library takes care of setting up the server, listening to the port and mapping the route. The response for the request is the same as the one generated by the Asp.Net api.
To fix this issue, David Fowler, a Microsoft software architect designed a way to create Asp.Net web apis with low code. In fact, you can do it with a comparable amount of code than with Node. It’s called FeatherHTTP and it’s available here.
To use it, you must install the template and you can use it with dotnet new.
To install the template you can use this command:
When you do it, the option feather appears in dotnet new:
You can now create a new project with the command:
When the project is created, you can see that it’s much lighter than the standard code: there is only the csproj file and Program.cs with this code:
Much smaller, no? As you can see, it uses the new C#9 feature “Top level statements” to reduce a little bit the code. We can change the code to have the same output of the standard code:
Now we have something comparable to Node! I’ve used here another feature of C#9, Records, to reduce a little the code size.
As we can see, with FeatherHttp, we can reduce the code size for an Asp.Net web api app, but can we still use it for a more complex API ?
We’ll try to reproduce the code used in my HttpRepl article here.
The first step is to copy the Customer class to the project. We’ll convert it to a record:
Then we’ll copy the repository:
One note here: as we declared Customer as a Record, it’s immutable, so we cannot simply replace its data with the new data in the Update method. Instead, we create a new customer and replace the current one with the newly created in the customers list.
We also need the customers.xml file that can be obtained at https://github.com/bsonnino/HttpRepl/blob/main/Customers.xml and add it to the project by adding this clause in the csproj file:
Now, we will change the main program to create the API:
We are mapping the routes for all the desired actions. For each mapping, we’ve created a method to use when the corresponding route is being called:
- For GetAll, we are using the same method we’ve used with the WeatherForecast, getting all customers, serializing the array into a json string and writing the response
- To get just one customer, where the id is in the route, we get the id with http.Request.RouteValues.TryGet, get the customer and write the response
- To add and update the customer, we must get it from the body with await http.Request.ReadFromJsonAsync
(); Once we get it, we can add or update the customer repository - Delete is similar to getting just one customer, but we delete it from the repository, instead of returning it.
You can test the API by using the same methods we’ve used in the HttpRepl article, the API will be the same, but in this case, I’m not using the customers route. Another minor issue is that Swagger is still not available in FeatherHttp (https://github.com/featherhttp/framework/issues/34), but this doesn’t change the functionality at all.
FeatherHttp is not production ready, it’s still in alpha, but it shows a direction to lightweight Asp.Net web api applications. As you can see, all the ceremony from Asp.Net web api can be removed and you can finish with an app as small as a Node one, but written in C#.
All the source code for this article is at https://github.com/bsonnino/FeatherHttp