Created
September 9, 2016 23:47
-
-
Save cwiederspan/4851ecdd7c793cb480ea8fa190a7a15a to your computer and use it in GitHub Desktop.
Quick snippet to allow plain text POST data to an ASP.NET Core Web Api
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class PlainTextFormatter : IInputFormatter { | |
| public bool CanRead(InputFormatterContext context) { | |
| return context.HttpContext.Request.ContentType.ToLower() == "text/plain"; | |
| } | |
| public Task<InputFormatterResult> ReadAsync(InputFormatterContext context) { | |
| var reader = new StreamReader(context.HttpContext.Request.Body); | |
| var content = reader.ReadToEnd(); | |
| return Task.FromResult(InputFormatterResult.Success(content)); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public void ConfigureServices(IServiceCollection services) { | |
| // Add framework services. | |
| services.AddMvc(setup => { | |
| var formatter = new PlainTextFormatter(); | |
| setup.InputFormatters.Add(new PlainTextFormatter()); | |
| }); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment