Carter is an open-source library that sits on top of ASP.NET Core and allows us to build more elegant routing rather than ASP.NET Controllers, convention routing or attribute routing. It was created by John Shanon, one of the contributors to NancyFX. Therefore, many APIs that are available in Carter are in Nancy too. For that reason, if you have a project in NancyFX, it will be easy to convert into Carter.
- Model binding and validation – Fluent validation
- Before/after hooks
- Support for OpenAPI specification
Carter usage examples
You can add Carter to your project by NuGet Packages:
The second option is using a template of Carter:
Now you can check the contents of the created files and see how Carter was set up and used. First of all, look at Startup:
We have got two methods here. In ConfigureServices we literally add all the dependencies, so you can see there adding Carter dependency. Then in Configure, we are actually telling it to use Carter in building endpoints. Another created class is HomeModule:
You can see that Carter supports endpoint routing and all the extensions IEndpointConventionBuilder offers.
Carter gives us opportunities for using hooks. Before Hooks is used mainly for authorizing users and After Hooks for logging some information. Below you can see an example:
I found out about this framework by watching ASP.NET Community Standup and a conversation between Jon Galloway and Brady Gaster. After that, I started to get familiar with this library and it is really worth your attention, so I decided to mention it on my blog. This micro-framework provides building HTTP endpoints with more elegant and powerful routing. Additionally, Carter gives great possibilities to create validation. You can find more samples here. Enjoy and check out my other posts!