3
我的應用程序在遷移到beta 6/7後停止工作,在調查後,我發現我的json反序列化程序不再被使用(Jil),它被要求寫作但不適合閱讀。MvcOptions.InputFormatters無法在asp.net vnext beta7上工作
現在,我正在搜索論壇和閱讀aspnet代碼的3天,但我還沒有找到問題。
我注意到,JSON.net到處都用在測試6,在測試有點少7
這裏是我的代碼:
public void ConfigureServices(IServiceCollection services)
{
services.AddMvcCore(options =>
{
var jilFormatter = new JilMediaTypeFormatter();
options.OutputFormatters.Clear();
options.OutputFormatters.Add(jilFormatter);
options.InputFormatters.Clear();
options.InputFormatters.Add(jilFormatter);
options.FormatterMappings.SetMediaTypeMappingForFormat("json", MediaTypeHeaderValue.Parse("application/json"));
options.ModelBinders.Add(new DocumentModelBinder());
options.ModelBinders.Add(new DataTablesBinder());
});
services.AddDataProtection();
services.AddWebEncoders();
}
即使我做到這InputFormatters.Clear( )沒有添加對象,它保持反序列化的請求,我不知道它是如何做到這一點。
而且我JIL InputFormatter/OutputFormatter(在ouputformatter工作,我可以在CanWrite突破,但沒有任何反應了的CanRead)
internal class JilMediaTypeFormatter : IOutputFormatter, IInputFormatter
{
private static readonly string [] _readableTypes = new[] { "application/json", "text/json", "text/javascript" };
private static readonly Task<bool> _done = Task.FromResult(true);
private readonly Options _options = Options.RFC1123ExcludeNullsIncludeInherited;
public bool CanWriteResult(OutputFormatterContext context, Microsoft.Net.Http.Headers.MediaTypeHeaderValue contentType)
{
return contentType ==null || _readableTypes.Contains(contentType.MediaType.ToLowerInvariant());
}
public Task WriteAsync(OutputFormatterContext context)
{
context.HttpContext.Response.ContentType = "application/json";
using (var writer = new StreamWriter(context.HttpContext.Response.Body))
{
JSON.Serialize(context.Object, writer, _options);
writer.Flush();
return _done;
}
}
public bool CanRead(InputFormatterContext context)
{
return _readableTypes.Contains(context.HttpContext.Request.ContentType);
}
public Task<object> ReadAsync(InputFormatterContext context)
{
var reader = new StreamReader(context.HttpContext.Request.Body);
var result = JSON.Deserialize(reader, context.ModelType, _options);
return Task.FromResult(result);
}
}
只需點擊此... +10000000 –