Skip to content

Instantly share code, notes, and snippets.

@mika76
Created July 12, 2022 07:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mika76/f81603f66776d2e4fc486434fe1ed69d to your computer and use it in GitHub Desktop.
Save mika76/f81603f66776d2e4fc486434fe1ed69d to your computer and use it in GitHub Desktop.
CSVTextInputFormatter ASP.MVC
using System.IO;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc.Formatters;
using Microsoft.Net.Http.Headers;
namespace x;
public class CSVTextInputFormatter : TextInputFormatter
{
public CSVTextInputFormatter()
{
SupportedMediaTypes.Add(MediaTypeHeaderValue.Parse("text/csv"));
SupportedEncodings.Add(Encoding.UTF8);
SupportedEncodings.Add(Encoding.Unicode);
}
public override async Task<InputFormatterResult> ReadRequestBodyAsync(InputFormatterContext context, Encoding encoding)
{
var httpContext = context.HttpContext;
//var serviceProvider = httpContext.RequestServices;
//var logger = serviceProvider.GetRequiredService<ILogger<CSVTextFormatter>>();
using var reader = new StreamReader(httpContext.Request.Body, encoding);
return await InputFormatterResult.SuccessAsync(await reader.ReadToEndAsync());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment