C# JsonValueProviderFactory 抛出“请求太大”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9509721/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 07:42:09  来源:igfitidea点击:

JsonValueProviderFactory throws "request too large"

c#asp.net-mvcjson.netasp.net-mvc-4

提问by Keith Nicholas

I'm getting an exception that the JSON request was too large to be deserialized.

我收到一个异常,即 JSON 请求太大而无法反序列化。

It's coming from the JsonValueProviderFactory....

它来自 JsonValueProviderFactory....

The MVC App currently has a custom model binder using Json.Net which has no problem deserializing the json data. However I'm assuming the default JSON value provider is tripping up? or has some weird limit built into it?

MVC 应用程序目前有一个使用 Json.Net 的自定义模型绑定器,反序列化 json 数据没有问题。但是,我假设默认的 JSON 值提供程序出错了?还是有一些奇怪的限制?

It may be be to do with the latest release of MVC4 as when using the previous build of MVC4 there were no problems with large amounts of JSON.

这可能与 MVC4 的最新版本有关,因为在使用先前版本的 MVC4 时,大量 JSON 没有问题。

So, is there a way to change the setting s of the actual json value binder?

那么,有没有办法改变实际json值绑定器的设置?

going by http://haacked.com/archive/2011/06/30/whatrsquos-the-difference-between-a-value-provider-and-model-binder.aspx

通过http://haacked.com/archive/2011/06/30/whatrsquos-the-difference-between-a-value-provider-and-model-binder.aspx

I get the impression it's some custom thing that turns it into a dictionary....I can't find any source code related to it or if there are any settings I can change?

我的印象是它是一些自定义的东西,将它变成了字典......我找不到任何与它相关的源代码,或者是否有任何我可以更改的设置?

Or is there an alternative ValueBinder I could use?

或者是否有我可以使用的替代 ValueBinder?

or any other options?

或任何其他选择?

Server Error in '/' Application.
The JSON request was too large to be deserialized.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: The JSON request was too large to be deserialized.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[InvalidOperationException: The JSON request was too large to be deserialized.]
   System.Web.Mvc.EntryLimitedDictionary.Add(String key, Object value) +464621
   System.Web.Mvc.JsonValueProviderFactory.AddToBackingStore(EntryLimitedDictionary backingStore, String prefix, Object value) +413
   System.Web.Mvc.JsonValueProviderFactory.AddToBackingStore(EntryLimitedDictionary backingStore, String prefix, Object value) +164
   System.Web.Mvc.JsonValueProviderFactory.AddToBackingStore(EntryLimitedDictionary backingStore, String prefix, Object value) +164
   System.Web.Mvc.JsonValueProviderFactory.AddToBackingStore(EntryLimitedDictionary backingStore, String prefix, Object value) +373
   System.Web.Mvc.JsonValueProviderFactory.AddToBackingStore(EntryLimitedDictionary backingStore, String prefix, Object value) +164
   System.Web.Mvc.JsonValueProviderFactory.GetValueProvider(ControllerContext controllerContext) +116
   System.Web.Mvc.<>c__DisplayClassc.<GetValueProvider>b__7(ValueProviderFactory factory) +34

       System.Linq.WhereSelectEnumerableIterator`2.MoveNext() +151
   System.Linq.WhereSelectEnumerableIterator`2.MoveNext() +177

采纳答案by Darin Dimitrov

If you use JSON.NET for serialization/deserialization, you could substitute the default JsonValueProviderFactory with a custom one as shown in this blog post:

如果您使用 JSON.NET 进行序列化/反序列化,您可以使用自定义的 JsonValueProviderFactory 替换默认的 JsonValueProviderFactory,如本博客文章所示:

public sealed class JsonDotNetValueProviderFactory : ValueProviderFactory
{
   public override IValueProvider GetValueProvider(ControllerContext controllerContext)
   {
        if (controllerContext == null)
            throw new ArgumentNullException("controllerContext");

        if (!controllerContext.HttpContext.Request.ContentType.StartsWith("application/json", StringComparison.OrdinalIgnoreCase))
            return null;

        var reader = new StreamReader(controllerContext.HttpContext.Request.InputStream);
        var bodyText = reader.ReadToEnd();

        return String.IsNullOrEmpty(bodyText) ? null : new DictionaryValueProvider<object>(JsonConvert.DeserializeObject<ExpandoObject>(bodyText, new ExpandoObjectConverter()) , CultureInfo.CurrentCulture);
    }
}

and in your Application_Start:

并在您的Application_Start

ValueProviderFactories.Factories.Remove(ValueProviderFactories.Factories.OfType<JsonValueProviderFactory>().FirstOrDefault());
ValueProviderFactories.Factories.Add(new JsonDotNetValueProviderFactory());

and if you want to stick with the default factory which uses the JavaScriptSerializerclass you could adjust the maxJsonLengthproperty in your web.config:

如果你想坚持使用JavaScriptSerializer类的默认工厂,你可以maxJsonLength在你的 web.config 中调整属性:

<system.web.extensions>
    <scripting>
        <webServices>
            <jsonSerialization maxJsonLength="2147483644"/>
        </webServices>
    </scripting>
</system.web.extensions>

回答by Doug Wilson

For me, the maxJsonLength was not being exceeded. I had to add the following:

对我来说,没有超过 maxJsonLength。我必须添加以下内容:

<appSettings>
  <add key="aspnet:MaxJsonDeserializerMembers" value="150000" />
</appSettings>

It worked a treat after that.

在那之后它起作用了。

回答by Sam Rueby

The above suggestions didn't work for me until I tried this:

在我尝试之前,上述建议对我不起作用:

// Global.asax.cs    
protected void Application_Start() {
    MiniProfiler.Settings.MaxJsonResponseSize = int.MaxValue;
}