Processing text/plain in ASP.NET Web API

Seems straightforward right?  And it is.  The only missing piece is that your API doesn’t know what to do when a form body comes across as “Content-Type”: “text/plain”

Have no fear, dotnet core has a class named InputTextFormatter that is a base class for TextInputFormatter which has derived classes for working with JSON and XML.  So the below snippets will get you a basic implementation for dealing with plain text and how to add it to your MVC pipeline

And to make sure that this code is injected in your MVC pipeline.  In your Startup.cs or wherever you register your dependencies

And then inside of your controller

And that’s it.  You’ve successfully handled text/plain in your controller and can be reused in any other controller action that needs to have text/plain as the content type.

facebooktwittergoogle_plusredditpinterestlinkedinmail

About the author

Ben Pyle is a software developer who enjoys solving problems and delighting customers with right quality software. He's a husband, father to 2 boys and avid golfer. In his free time he enjoys reading, playing lots of golf and hanging out with his family doing whatever they are into.

Leave a Reply

Your email address will not be published. Required fields are marked *