Skip to content

Hi, My Name is Ben

  • About

Processing text/plain in ASP.NET Web API

August 7, 2018 by Benjamen

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

TextInputFormatter
C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
    public class TextPlainFormatter : TextInputFormatter
    {
        public TextPlainFormatter()
        {
            SupportedMediaTypes.Add("text/plain");
        }
 
        protected override bool CanReadType(Type type)
        {
            return type == typeof(string);
        }
 
        public override async Task<InputFormatterResult> ReadAsync(InputFormatterContext context)
        {
            string data = null;
            using (var streamReader = new StreamReader(
                context.HttpContext.Request.Body))
 
            {
                data = await streamReader.ReadToEndAsync();
            }
 
            return InputFormatterResult.Success(data);
        }
 
        public override async Task<InputFormatterResult> ReadRequestBodyAsync(InputFormatterContext context, Encoding encoding)
        {
            throw new NotImplementedException();
        }
    }

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

C#
1
2
3
4
    public void ConfigureServices(IServiceCollection services)
    {
       services.AddMvc(o => o.InputFormatters.Add(new TextPlainFormatter()));
    }

And then inside of your controller

Controller
C#
1
2
3
4
5
6
        [HttpPost]
        [Route("[controller]")]
        public async Task<IActionResult> Post([FromBody] string content)
        {
            // do something with content
        }

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

Post navigation

Previous Post:

AWS SNS HTTP POST to C# (dotnet core 2.1)

Next Post:

Simple ModelState Extension

Leave a Reply Cancel reply

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

Recent Posts

  • Spring Boot, Logback and Logstash
  • Covariant Method Return Types in Java
  • From C# to Java
  • Time series data in MongoDB
  • Documenting your ASP.NET Core Web API with Swashbuckle

Recent Comments

  • Benjamen on From C# to Java
  • Mike Graf on From C# to Java
  • DJ on Takeaways from my First Analytics Conference

Archives

  • March 2019
  • December 2018
  • September 2018
  • August 2018
  • December 2017
  • November 2017
  • October 2017
  • March 2016
  • February 2016
  • November 2015

Categories

  • Agile
  • AWS
  • Data
  • Java
  • Life
  • Programming
  • Tech
  • Uncategorized

Meta

  • Log in
  • Entries RSS
  • Comments RSS
  • WordPress.org
© 2021 Hi, My Name is Ben | WordPress Theme by SuperbThemes