Created
July 12, 2022 07:31
-
-
Save mika76/f81603f66776d2e4fc486434fe1ed69d to your computer and use it in GitHub Desktop.
CSVTextInputFormatter ASP.MVC
This file contains 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
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