Asp.Net JsonResult重写

在Json序列化工具中,Newtonsoft.Json 的工具包相对比较好用。

对于循环引用、序列化格式、时间格式等都提供了可配置。

 

如果想重写Mvc 自带 JsonResult 返回结果,提供了2中处理方式

1.使用扩展字段data,多一层处理,这种方式比较简单

/// <summary>
/// json 处理
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public new JsonResult Json(object obj)
{
    //可使用配置处理
    string json = JsonConvert.SerializeObject(obj, new JsonSerializerSettings
    {
        ReferenceLoopHandling = ReferenceLoopHandling.Ignore
    });

    object data = new
    {
        data=json
    };

    return base.Json(obj, JsonRequestBehavior.AllowGet);
}

 

2.使用重写JsonResult 类方式

详情:http://www.cnblogs.com/tianma3798/p/5596703.html

 

更多:

Asp.Net Mvc控制器重名问题整理

Asp.Net MVC--Controller激活2

MVC中HttpContext, HttpContextBase, HttpContextWrapper联系

posted @ 2018-04-24 11:36  天马3798  阅读(508)  评论(0编辑  收藏  举报