$.ajax maxjsonlength,MaxJsonLength exception in ASP.NET MVC during JavaScriptSerializer

该博客详细介绍了如何在ASP.NET MVC5中遇到JSON请求数据长度超出限制的问题时,通过创建自定义的JsonValueProviderFactory来解决。作者修改了全局.cs文件,增加了web.config配置,并提供了两个类:CustomJsonValueProviderConfig和CustomJsonValueProviderFactory,这两个类允许根据web.config中的设置动态调整MaxJsonLength和MaxJsonDeserializerMembers的最大值,从而避免抛出长度超出错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Alternative ASP.NET MVC 5 Fix:

In my case the error was occurring during the request. Best approach in my scenario is modifying the actual JsonValueProviderFactory which applies the fix to the global project and can be done by editing the global.cs file as such.

JsonValueProviderConfig.Config(ValueProviderFactories.Factories);

add a web.config entry:

and then create the two following classes

public class JsonValueProviderConfig

{

public static void Config(ValueProviderFactoryCollection factories)

{

var jsonProviderFactory = factories.OfType().Single();

factories.Remove(jsonProviderFactory);

factories.Add(new CustomJsonValueProviderFactory());

}

}

This is basically an exact copy of the default implementation found in System.Web.Mvc but with the addition of a configurable web.config appsetting value aspnet:MaxJsonLength.

public class CustomJsonValueProviderFactory : ValueProviderFactory

{

/// Returns a JSON value-provider object for the specified controller context.

/// A JSON value-provider object for the specified controller context.

/// The controller context.

public override IValueProvider GetValueProvider(ControllerContext controllerContext)

{

if (controllerContext == null)

throw new ArgumentNullException("controllerContext");

object deserializedObject = CustomJsonValueProviderFactory.GetDeserializedObject(controllerContext);

if (deserializedObject == null)

return null;

Dictionary strs = new Dictionary(StringComparer.OrdinalIgnoreCase);

CustomJsonValueProviderFactory.AddToBackingStore(new CustomJsonValueProviderFactory.EntryLimitedDictionary(strs), string.Empty, deserializedObject);

return new DictionaryValueProvider(strs, CultureInfo.CurrentCulture);

}

private static object GetDeserializedObject(ControllerContext controllerContext)

{

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

return null;

string fullStreamString = (new StreamReader(controllerContext.HttpContext.Request.InputStream)).ReadToEnd();

if (string.IsNullOrEmpty(fullStreamString))

return null;

var serializer = new JavaScriptSerializer()

{

MaxJsonLength = CustomJsonValueProviderFactory.GetMaxJsonLength()

};

return serializer.DeserializeObject(fullStreamString);

}

private static void AddToBackingStore(EntryLimitedDictionary backingStore, string prefix, object value)

{

IDictionary strs = value as IDictionary;

if (strs != null)

{

foreach (KeyValuePair keyValuePair in strs)

CustomJsonValueProviderFactory.AddToBackingStore(backingStore, CustomJsonValueProviderFactory.MakePropertyKey(prefix, keyValuePair.Key), keyValuePair.Value);

return;

}

IList lists = value as IList;

if (lists == null)

{

backingStore.Add(prefix, value);

return;

}

for (int i = 0; i < lists.Count; i++)

{

CustomJsonValueProviderFactory.AddToBackingStore(backingStore, CustomJsonValueProviderFactory.MakeArrayKey(prefix, i), lists[i]);

}

}

private class EntryLimitedDictionary

{

private static int _maximumDepth;

private readonly IDictionary _innerDictionary;

private int _itemCount;

static EntryLimitedDictionary()

{

_maximumDepth = CustomJsonValueProviderFactory.GetMaximumDepth();

}

public EntryLimitedDictionary(IDictionary innerDictionary)

{

this._innerDictionary = innerDictionary;

}

public void Add(string key, object value)

{

int num = this._itemCount + 1;

this._itemCount = num;

if (num > _maximumDepth)

{

throw new InvalidOperationException("The length of the string exceeds the value set on the maxJsonLength property.");

}

this._innerDictionary.Add(key, value);

}

}

private static string MakeArrayKey(string prefix, int index)

{

return string.Concat(prefix, "[", index.ToString(CultureInfo.InvariantCulture), "]");

}

private static string MakePropertyKey(string prefix, string propertyName)

{

if (string.IsNullOrEmpty(prefix))

{

return propertyName;

}

return string.Concat(prefix, ".", propertyName);

}

private static int GetMaximumDepth()

{

int num;

NameValueCollection appSettings = ConfigurationManager.AppSettings;

if (appSettings != null)

{

string[] values = appSettings.GetValues("aspnet:MaxJsonDeserializerMembers");

if (values != null && values.Length != 0 && int.TryParse(values[0], out num))

{

return num;

}

}

return 1000;

}

private static int GetMaxJsonLength()

{

int num;

NameValueCollection appSettings = ConfigurationManager.AppSettings;

if (appSettings != null)

{

string[] values = appSettings.GetValues("aspnet:MaxJsonLength");

if (values != null && values.Length != 0 && int.TryParse(values[0], out num))

{

return num;

}

}

return 1000;

}

}

资源下载链接为: https://pan.quark.cn/s/67c535f75d4c 在Android开发中,为了提升用户体验和视觉效果,背景模糊化处理是一种常用的设计手段。它可以为应用界面增添层次感,同时突出显示主要内容。本文将详细介绍如何在Android中实现背景模糊化功能。 首先,我们需要获取当前设备的壁纸作为背景。这可以通过WallpaperManager类来完成。调用WallpaperManager.getInstance(this.getContext())可以获取壁纸管理器实例,然后通过getDrawable()方法获取当前壁纸的Drawable对象。接下来,需要将壁纸Drawable转换为Bitmap对象,因为模糊处理通常需要在Bitmap上进行。可以通过((BitmapDrawable) wallpaperDrawable).getBitmap()来完成这一转换。 模糊处理的核心是使用Android的RenderScript API。RenderScript是一种高效的并行计算框架,特别适合处理图像操作。在blur()方法中,我们创建了一个RenderScript实例,并利用ScriptIntrinsicBlur类来实现模糊效果。ScriptIntrinsicBlur提供了设置模糊半径(setRadius(radius))和执行模糊操作(forEach(output))的方法。模糊半径radius可以根据需求调整,以达到期望的模糊程度。 然而,仅依赖ScriptIntrinsicBlur可能无法达到理想的模糊效果,因此我们还需要对原始图片进行缩放处理。为此,我们设计了small()和big()方法。先将图片缩小(small()),然后执行模糊操作,最后再将图片放大(big())。这种方式不仅可以增强模糊效果,还能在一定程度上提高处理速度。在small(
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值