更改默认JSON序列应用于ASP MVC3 [英] Change Default JSON Serializer Used In ASP MVC3

查看:94
本文介绍了更改默认JSON序列应用于ASP MVC3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个控制器,正在返回大JSON对象jQuery的海军报,我不知道这将是多么容易的东西更快像ServiceStack.Text一个替换默认的JavaScriptSerializer。

I have a controller that is returning large JSON objects to jQuery Flot and I was wondering how easy it would be to replace the default JavaScriptSerializer with something faster like the one from ServiceStack.Text.

这将是很好的,如果我可以改变这样的东西用DependencyResolver,但我想,如果绝对一切就解决了,这是,它可以得到pretty缓慢。

It would be good if I could change stuff like this using the DependencyResolver, but I suppose if absolutely everything was resolved this was, it could get pretty slow.

推荐答案

最好的办法是从JsonResult类继承并重写Execute方法像

your best bet is to inherit from JsonResult class and override Execute method like

public class CustomJsonResult: JsonResult
{
    public CustomJsonResult()
    {
       JsonRequestBehavior = JsonRequestBehavior.DenyGet;
    }
    public override void ExecuteResult(ControllerContext context) {
            if (context == null) {
                throw new ArgumentNullException("context");
            }
            if (JsonRequestBehavior == JsonRequestBehavior.DenyGet &&
                String.Equals(context.HttpContext.Request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase)) {
                throw new InvalidOperationException(MvcResources.JsonRequest_GetNotAllowed);
            }

            HttpResponseBase response = context.HttpContext.Response;

            if (!String.IsNullOrEmpty(ContentType)) {
                response.ContentType = ContentType;
            }
            else {
                response.ContentType = "application/json";
            }
            if (ContentEncoding != null) {
                response.ContentEncoding = ContentEncoding;
            }
            if (Data != null) {
                CustomJsSerializer serializer = new CustomJsSerializer();
                response.Write(serializer.Serialize(Data));
            }
        }
}

code从JsonResult类MVC3的拍摄,改变了这行

code is taken from JsonResult class of mvc3 and changed this line

JavaScriptSerializer serializer = new JavaScriptSerializer();

CustomJsSerializer serializer = new CustomJsSerializer();

您可以使用这个类的操作方法一样。

you can use this class in action method like

public JsonResult result()
{
    var model = GetModel();
    return new CustomJsonResult{Data = model};
}

此外,你可以覆盖控制器类的JSON方法在你基地的控制器像

Additionally you can override json method of Controller class in your Base controller like

public class BaseController:Controller
{
   protected internal override JsonResult Json(object data)
        {
            return new CustomJsonResult { Data = data };
        }
}

现在,如果你有一个从BaseController那么所有的控制器返回JSON(数据)会打电话给你的序列化方案。也有您可以选择覆盖的Json 方法的其他重载。

now if you have all your controllers from BaseController then return Json(data) will call your serialization scheme. There are also other overloads of Json method that you may choose to override.

这篇关于更改默认JSON序列应用于ASP MVC3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆