Custom OutputFormatters in ASP.NET Core 3.0

Today, I faced the nastiest bug 🐜 in my code, when upgrading a Web API project from 2.2. to 3.0.

Image for post
Image for post
The API response was consistently an MS Excel file.

The API response was consistently an MS Excel file, no matter what the request’s Accept header was.

I knew the API had multiple output formatters, and it worked well in 2.2, so what could be wrong? My MVC setup in my Startup.cs looked like:

This turned out to be the reason, and it was surprising because it was never a problem before.

services.AddMvc(...)
.AddJsonOptions(...);
services.AddControllers()
.AddNewtonsoftJson(...);
services.AddMvc(options => {
options.OutputFormatters.Add(...);
})

I’ve learned I don’t know anything. I've also learned that people will pay for what I know. Maybe that's why they never pay.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store