【C#】mvc的json长度配置
mvc的请求最大长度,响应最大长度,请求序列化json最大长度,响应序列化json最大长度是分开的。
mvc响应序列化方法可能读不默认到配置的最大长度,可以重写序列化方法。
<system.web> <authentication mode="None" /> <compilation debug="true" targetFramework="4.8" /> <!--http请求参数最大长度maxRequestLength--> <httpRuntime targetFramework="4.5" maxRequestLength="102400"/> </system.web> <system.web.extensions> <scripting> <webServices> <!--mvc响应序列化里读不到json序列化长度,重写Controller序列化方法并使用重写的Json响应序列化最大长度<add key="MVCmaxJsonLength" value="2147483644"/>--> <jsonSerialization maxJsonLength="2147483644"/> </webServices> </scripting> </system.web.extensions> <appSettings> <!--Controller重写的Json响应序列化最大长度--> <add key="MVCmaxJsonLength" value="2147483644"/> </appSettings>
重写Controller响应的Json序列化方法
public class ControllerExtensions :Controller { public new JsonResult Json(object data, JsonRequestBehavior be) { var res = base.Json(data); if (!string.IsNullOrWhiteSpace(ConfigurationHelper.AppSetting("MVCmaxJsonLength"))) { int max = 0; int.TryParse(ConfigurationHelper.AppSetting("MVCmaxJsonLength"), out max); res.MaxJsonLength = max > 0 ? max : res.MaxJsonLength; } res.JsonRequestBehavior = be; return res; } public new JsonResult Json(object data) { var res = base.Json(data); if (!string.IsNullOrWhiteSpace(ConfigurationHelper.AppSetting("MVCmaxJsonLength"))) { int max = 0; int.TryParse(ConfigurationHelper.AppSetting("MVCmaxJsonLength"), out max); res.MaxJsonLength = max > 0 ? max : res.MaxJsonLength; } return res; } }