Я следую этому вики , чтобы использовать DataContractJsonSerializer
для десериализации данных запроса типов носителей json, но это для .net framework, я ищу тот же путь для .net core web api.
Мне нужно обновить значение по умолчанию JsonMediaTypeFormatter
до DataContractJsonSerializer
вместо Json.NET.Я знаю, что я должен внести изменения в Startup.cs в
[public void ConfigureServices(IServiceCollection services)],
, но не могу найти, какой класс является соответствующей частью GlobalConfiguration.Configuration.Formatters.JsonFormatter
Я считаю, что мне нужно реализовать класс Inputformatter:
public class CustomizedJsonInputFormatter : InputFormatter
{
public CustomizedJsonInputFormatter()
{
// this.SupportedMediaTypes.Clear();
this.SupportedMediaTypes.Add(MediaTypeHeaderValue.Parse("application/json"));
}
protected override bool CanReadType(Type type)
{
if(type == typeof(CreateJobRequest))
{
return base.CanReadType(type);
}
return false;
}
public override async Task<InputFormatterResult> ReadRequestBodyAsync(InputFormatterContext context)
{
var type = context.ModelType;
var request = context.HttpContext.Request;
try
{
MediaTypeHeaderValue requestContentType = null;
MediaTypeHeaderValue.TryParse(request.ContentType, out requestContentType);
DataContractJsonSerializer serializer = new DataContractJsonSerializer(type);
object result = serializer.ReadObject(context.HttpContext.Request.Body);
return await InputFormatterResult.SuccessAsync(result);
}
catch
{
return await InputFormatterResult.FailureAsync();
}
}
}
Но я получаю ошибку 204 Нет содержимого при использовании почтальона для отправки запроса получения, фактически метод контроллера обработал запрос успешно.