ASP.NET Web API template

An opinionated ASP.NET Web API template for the dotnet new command

Posted by Alfus Jaganathan on Monday, March 14, 2022

If you are a .NET engineer looking for building a cool Web API (REST), this article is for you.

Background

Few years ago, me and Cedric Yao created a simple dotnet new web api template. I personally found it very useful in quickly bootstrapping a simple ASP.NET Core Web API application, especially while working with several customers. I have also received positive feedbacks from the users of it, together with some suggestions and improvements. This encouraged me to spent some of my personal hours on it, so as to help the community.

Keeping the existing one, a simple template by itself, I started creating another one with some cool features, patterns and practices, which I learnt along the journey of building .NET applications. And finally, I was able to publish it today. You can find it here

This template can help developers to start writing code for business features, right after using it, which is key takeaway that you get out it.

Other features that this template contains within it are …

  • Well tested (built using TDD). In addition, all available unit & integration tests can also be used as code references for engineers who are new to writing unit/integration tests.

  • Psake, a powershell based build automation tool is used, which reduces some of the dependencies of CI tools. You can learn more about the tool here.

  • Unit tests are integrated to generate Cobertura Code Coverage results.

  • MediatR, a simple mediator implementation for .NET. I love the usage of this tool which reduces tight coupling between code components.

  • Autofac is used in addition to .NET framework provided ServiceCollection as Inversion of Control container

  • Serilog is used for structured logging capability and custom injection of additional properties for better diagnostics.

  • In addition to xml documentation, APIs are well documented using Swagger Annotations, a testable way of documentation, rather than xml based documentation.

  • Uses Uri based API versioning, learn more here

  • Usage of sample Request and Response headers. Optional to modify as needed, based on the need.

  • Standardized error responses, using ProblemDetails

  • High-performance logging in .NET

  • Fluent validation, instead of ASP.NET framework model validation

  • Fluent Assertions, XUnit, Moq and Microsoft Testing SDK for unit and integration tests.

  • Usage of a global versioniong of all nuget dependencies across the projects.

  • Steeltoe Application Configuration is used for auto wiring of configurations/secrets from Kubernetes and Cloud Foundry. References included in Program.cs

  • ASP.NET Health checks, a sample implementation included.

  • Docker ready.

Want to try? Please follow the readme here

Hope it helps!

Quick References

Template

Source

Love helping the community!


comments powered by Disqus