简体   繁体   English

如何在 .net 内核中设置最大 Json 长度

[英]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.

相关问题 Json Object Asp.net 核心 3.1 的最大长度 - Max Length for Json Object Asp.net Core 3.1 如何设置datagridview列的最大长度 - How to set max length of datagridview column 如何设置TextBox的最大长度限制? - How to set limit of max length to TextBox? 如何在 asp.net 内核 3 中设置 json 串行器设置? - How to set json serializer settings in asp.net core 3? 如何在 project.json 中为 .NET Core 设置根命名空间? - How to set the root namespace in project.json for .NET Core? ASP.NET 核心与 AWS ElasticBeanstalk - 有没有办法使用 appsettings.json 设置 nginx 配置 client_max_body_size 属性? - ASP.NET Core with AWS ElasticBeanstalk - Is there a way to set nginx config client_max_body_size property using the appsettings.json? 有没有一种方法可以测试返回对象的JSON长度是否超过最大长度,然后才能在AJAX.NET中引发异常? - Is there a way to test if a return object JSON length exceeds max length before it throws exception in AJAX.NET? 如何在 .net core 中反序列化这个 Json? - How to deserialize this Json in .net core? 如果我已经设置了默认值,如何指定最大长度字符串? - How to specify max length string if I have a default already set? asp.net 显示指定密钥的核心 2 太长; 最大密钥长度为 3072 字节 - asp.net core 2 showing specified key was too long; max key length is 3072 bytes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM