1

我已将 Newton Soft 添加到组合中,并使用此博客作为模板: http ://wingkaiwan.com/2012/12/28/replacing-mvc-javascriptserializer-with-json-net-jsonserializer/

我现在有:

public class BaseJsonController : BaseController
{
    protected override JsonResult Json(object data, string contentType, Encoding contentEncoding, JsonRequestBehavior behavior)
    {
        return new JsonNetResult
        {
            Data = data,
            ContentType = contentType,
            ContentEncoding = contentEncoding,
            JsonRequestBehavior = behavior
        };
    }
}

public class JsonNetResult : JsonResult
{
    public JsonNetResult()
    {
        Settings = new JsonSerializerSettings
        {
            ReferenceLoopHandling = ReferenceLoopHandling.Error
        };
    }

    public JsonSerializerSettings Settings { get; private set; }

    public override void ExecuteResult(ControllerContext context)
    {
        if (context == null)
            throw new ArgumentNullException("context");
        if (this.JsonRequestBehavior == JsonRequestBehavior.DenyGet && string.Equals(context.HttpContext.Request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase))
            throw new InvalidOperationException("JSON GET is not allowed");

        HttpResponseBase response = context.HttpContext.Response;
        response.ContentType = string.IsNullOrEmpty(this.ContentType) ? "application/json" : this.ContentType;

        if (this.ContentEncoding != null)
            response.ContentEncoding = this.ContentEncoding;
        if (this.Data == null)
            return;

        var scriptSerializer = JsonSerializer.Create(this.Settings);

        using (var sw = new StringWriter())
        {
            scriptSerializer.Serialize(sw, this.Data);
            response.Write(sw.ToString());
        }
    }
}

现在,在我的控制器中,我这样做了:

[HandleError]
public class ProcurementActionsController : BaseJsonController
{
    ...

    [GridAction]
    [AcceptVerbs(HttpVerbs.Post)]
    public JsonResult AjaxGetAll(string pageFilter = null, string searchTerm = null)
    {
        var rawData = GetProcurementActions(pageFilter);
        return new JsonNetResult 
        {
            Data = new GridModel { Data = rawData },
            JsonRequestBehavior = JsonRequestBehavior.AllowGet,
            Settings = { ReferenceLoopHandling = ReferenceLoopHandling.Ignore }
        };
    }

    ...

}

但是,我仍然遇到同样的错误!根据堆栈跟踪,它仍在尝试使用JavaScriptSerializer而不是JsonSerializer

[InvalidOperationException: Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property.]
   System.Web.Script.Serialization.JavaScriptSerializer.Serialize(Object obj, StringBuilder output, SerializationFormat serializationFormat) +188
   System.Web.Script.Serialization.JavaScriptSerializer.Serialize(Object obj) +56
   System.Web.Mvc.JsonResult.ExecuteResult(ControllerContext context) +418
   System.Web.Mvc.<>c__DisplayClass14.<InvokeActionResultWithFilters>b__11() +31
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation) +656883
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation) +656883
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +254
   System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +658100
   System.Web.Mvc.Controller.ExecuteCore() +125
   System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__4() +48
   System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +21
   System.Web.Mvc.Async.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _) +15
   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +85
   System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +51
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +454
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +263

是因为异步调用吗?如果是这样,我需要在我的 BaseJsonController 中覆盖什么才能让它调用正确的 ExecuteResult(...) 方法?

4

2 回答 2

0

In MVC 4 and 5 this should be fixed easily by parameter in the web config file.

I had the same problem in MVC3 and just fixed it by replacing ValueProviderFactories in the app_start, following this thread:

http://forums.asp.net/t/1751116.aspx

于 2013-12-04T14:33:46.890 回答
0
    [GridAction]
    [AcceptVerbs(HttpVerbs.Post)]
    public JsonResult AjaxGetAll(string pageFilter = null, string searchTerm = null)
    {
        var rawData = GetProcurementActions(pageFilter);
        var gridData = new GridModel { Data = rawData };
        var json = JsonConvert.SerializeObject(gridData).Replace("\"Total\":0,\"Aggregates\":null", String.Format("\"Total\":{0},\"Aggregates\":null", rawData.Count));

        return new ContentResult { Content = json, ContentType = "application/json" };
    }
  1. 从数据库中获取数据。
  2. 需要将 ViewModel (List) 转换为 Telerik 的 GridModel 对象。
  3. 使用 Newton Soft 的 JsonSerializer 将 GridModel 转换为 JSON 字符串(这里没有长度限制!) Telerik Grid 需要知道总记录,并且由于某种原因它不在 JSON 中。通过 Replace() 方法更新它。
  4. 使用 ContentResult 对象而不是 ActionResult 或 JsonResult。有了这个,您可以定义自己的数据有效负载。

这种精确解决方案的唯一缺点是,如果它最终达到 4GB 左右。到那时,它可能会使大多数浏览器崩溃,除非您有办法对返回进行分块。我会添加一个最大长度检查并抛出一个错误。

于 2013-12-05T18:50:12.223 回答