[英]How to Set Max Json Length in .net core
Before .net core technology we can add maxjson size in web.config.but in .net core how to set max json size?在 .net 核心技术之前我们可以在 web.config 中添加 maxjson 大小,但是在 .net 核心如何设置最大 Z6438DEEC16ECDF2FC4 and where?在哪里?
You would need to manage this at the serialization level.您需要在序列化级别进行管理。 Not sure if you are the new Microsoft Json or NetwtonSoft json不确定您是新的 Microsoft Json 还是 NetwtonSoft json
When Microsoft you need to use the DataContractJsonSerializer , when NetwontSoft have a look at mel-green's solution , you can substitute the progress with bytes size and throw an error if to big.当 Microsoft 需要使用DataContractJsonSerializer时, NetwontSoft 看看 mel-green 的解决方案时,您可以用字节大小替换进度,如果大则抛出错误。 another option is to use options and set your own ITraceWriter that doesn't trade but throw an error when to big.另一种选择是使用选项并设置您自己的不交易但在大时抛出错误的ITraceWriter 。
A tip, when using Json you can decorate properties as to you can shorten the property names.提示,使用 Json 时,您可以装饰属性以缩短属性名称。 like so:像这样:
[JsonProperty("A1")]
private int SomeReallyLongPropertyName;
Make sure though not to make duplicate names, json will then map via the alias.确保虽然不要重复名称,但 json 将通过别名 map 然后。
another way to make the json shorter is by converting it into something smaller.使 json 更短的另一种方法是将其转换为更小的东西。
[JsonConverter(typeof(UTCDateTimeConverter))]
[JsonProperty("DT")]
public DateTime Date { get; set; }
the converter:转换器:
public sealed class UTCDateTimeConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return objectType == typeof(DateTime?);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (reader.Value is null) return null;
try
{
return new DateTime((long)reader.Value);
}
catch
{
if (DateTime.TryParse(reader.Value.ToString(), out DateTime d))
return d;
else
return null;
}
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
var val = ((DateTime)value).Ticks;
writer.WriteValue(val);
}
}
You can also decorate the class you like to convert to warn if the size is to large by implementing the above and change the implementation of WriteJson to monitor your size however that not what you asked as it it doesn't address the whole stream.您还可以通过执行上述操作来装饰您想要转换的 class 以警告大小是否过大,并更改 WriteJson 的实现以监视您的大小,但这不是您所要求的,因为它没有解决整个 stream。
You can handle this at serialization level.您可以在序列化级别处理此问题。 Have you tried doing something like below?您是否尝试过执行以下操作?
// Creates an instance of your JavaScriptSerializer
// and Setting the MaxJsonLength
var serializer = new JavaScriptSerializer() { MaxJsonLength = 86753090 };
// Perform your serialization
serializer.Serialize("Your JSON Contents");
Before .net core technology we can add maxjson size in web.config .but in .net core how to set max json size?在.net core技术之前,我们可以在web.config中添加maxjson大小。但是在.net core中,如何设置maxjson大小? and where?在哪里?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.