BaseApiController

Our .NET Web API controllers get their job done; however, they're all affected by a noticeable flaw--the amount of duplicate code we used to make them all behave in the same way, although with different entities. More specifically, this means the following:

  • They all have a DbContext property and a constructor that retrieves it through dependency injection
  • They all create a number of JsonSerializerSettings objects configured in the same way

This is a rather common issue when working with the MVC pattern, where the same interfaces and approaches are often adopted multiple times and within different controllers.

Luckily enough, repeating a behavior doesn't necessarily mean repeating the code; we can easily cut these dupes ...

Get ASP.NET Core 2 and Angular 5 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.