我在MVC中遵循代码。在我的POST操作中,params
将拥有大量数据。因此,我相应地更改了web.config
,但是却出现 ERROR 。实际问题是调用POST时控制器甚至没有命中。
I tried Following ways
base64
中收到 NULL 。controller.cs
[System.Web.Mvc.HttpPost]
public bool postImage(string base64)
{
return true;
}
web.config
<system.web.extensions>
<scripting>
<webServices>
<jsonSerialization maxJsonLength="2147483644"/>
</webServices>
</scripting>
JavaScript
$.ajax({
type: "POST",
url: 'http://localhost:21923/communities/postImage?base64=',
contentType: "application/json; charset=utf-8",
data: JSON.stringify(data),
dataType: "json",
success: function(data) {
document.getElementById('test').click();
},
error: function(a, b, c) {
console.log(a);
}
})
错误
使用JSON JavaScriptSerializer进行序列化或反序列化时出错。字符串的长度超过了在maxJsonLength属性上设置的值。\ r \ n参数名称:input
答案 0 :(得分:0)
创建一个JsonDotNetValueProviderFactory.cs
并编写以下代码:
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);
}
}
并在Global.asax.cs
的{{1}}中添加以下两行
Application_Start()
来源:Link
如果上述代码失败:遵循This可能会很有帮助
感谢dkinchen!