使用MVC之后, 默认的ActionResult 有很多子类譬如 JsonResult之类, 可以很方便. 基本用法如下:

        public ActionResult GetVacation()
        {
            var dt = ...(省略);
            if (dt == null || dt.Rows.Count==0) return Json(new { success = false, msg = "相应空逻辑!" }); ;
            return Json(new { 
                success = true,
                ...(省略)
            });//省略内容
        }

默认只能采用POST方式调用方法, Get 不行, 需要构建 JsonRequestBehavior 为AllowGet 方式. 返回 response.

        public JsonResult W1ArchiveOTUpdate(string xxx1, string xxx2)
        {
            JsonResult response = new JsonResult() { JsonRequestBehavior = JsonRequestBehavior.AllowGet };
            var ret = Biz.XXX(Request.Cookies["XXX"].Value);
            response.Data = new { success = ret.Success, msg = ret.Msg };
            return response;
        }

另一种情况, 返回数据行过多, 数据超过了默认限定, 记不清多少了, 所以采用了一个JsonResult 子类来定义到int.MAX. 如果还不行, 请考虑业务问题, 能分页就分页把.

过度方案:

    public class LargeJsonResult : JsonResult
    {
        const string JsonRequest_GetNotAllowed = "This request has been blocked because sensitive information could be disclosed to third party web sites when this is used in a GET request. To allow GET requests, set JsonRequestBehavior to AllowGet.";
        public LargeJsonResult()
        {
            MaxJsonLength = int.MaxValue;//102400;
            RecursionLimit = 100;
        }         public new int MaxJsonLength { get; set; }
        public new int RecursionLimit { get; set; }         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(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)
            {
                JavaScriptSerializer serializer = new JavaScriptSerializer() { MaxJsonLength = MaxJsonLength, RecursionLimit = RecursionLimit };
                response.Write(serializer.Serialize(Data));
            }
        }
    }
    
    
    //调用方式
         public ActionResult XXXX(string XXX1, string XXX2)
        {
            var ret = Biz.XXX();
            return new LargeJsonResult
            {
                Data = ret.Data
            };
            //return Json(ret);
        }

参考:

http://www.cnblogs.com/lmfeng/p/3596175.html

https://blog.csdn.net/zerorm/article/details/51488152

MVC JsonResult 结果返回的更多相关文章

  1. MVC JsonResult的用法

    本文导读:当客户端调用某个Action方法并希望以JSON的格式返回请求的数据时,ASP.NET MVC需要有一种机制将CLR对象转换成JSON格式予以响应,而这可以通过JsonResult来解决.下 ...

  2. Asp.net MVC 中Controller返回值类型ActionResult

    [Asp.net MVC中Controller返回值类型] 在mvc中所有的controller类都必须使用"Controller"后缀来命名并且对Action也有一定的要求: 必 ...

  3. ASP.NET MVC – 关于Action返回结果类型的事儿(上)

    原文:ASP.NET MVC – 关于Action返回结果类型的事儿(上) 本文转自:博客园-文超的技术博客 一.         ASP.NET MVC 1.0 Result 几何? Action的 ...

  4. System.Web.Mvc.JsonResult.cs

    ylbtech-System.Web.Mvc.JsonResult.cs 1.程序集 System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicK ...

  5. .net mvc web api 返回 json 内容,过滤值为null的属性

    原文:http://blog.csdn.net/xxj_jing/article/details/49508557 版权声明:本文为博主原创文章,未经博主允许不得转载. .net mvc web ap ...

  6. Spring MVC 3.0 返回JSON数据的方法

    Spring MVC 3.0 返回JSON数据的方法1. 直接 PrintWriter 输出2. 使用 JSP 视图3. 使用Spring内置的支持// Spring MVC 配置<bean c ...

  7. MVC web api 返回JSON的几种方式,Newtonsoft.Json序列化日期时间去T的几种方式。

    原文链接:https://www.muhanxue.com/essays/2015/01/8623699.html MVC web api 返回JSON的几种方式 1.在WebApiConfig的Re ...

  8. ASP.NET误人子弟教程:在MVC下如何返回图片

    这几天忙着一些小事,也没有写什么了,今天,我们来玩一个比较简单的东东.就是在MVC下如何返回图片,相信,在传统WebForm下,大家都晓得怎么弄,方也不限于一种,但是,在架构较为严格的MVC里面,刚开 ...

  9. 列举mvc ActionResult的返回值

    8.列举ASP.NET MVC ActionResult的返回值有几种类型? 主要有View(视图).PartialView(部分视图).Content(内容).Json(Json字符串).Javas ...

随机推荐

  1. Adjacency matrix based Graph

    Interface AddVertex(T data) AddEdge(int from, int to) DFS BFS MST TopSort PrintGraph using System; u ...

  2. Function program language

    历史 Lambda演算为描述函数及其评估提供了理论框架.它是一种数学抽象而不是编程语言 - 但它构成了几乎所有当前函数式编程语言的基础.等效的理论公式,组合逻辑,通常被认为比lambda演算更抽象,并 ...

  3. TableLaout

    <?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android=" ...

  4. 第二次Scrum冲刺——Life in CCSU

    第二次Scrum冲刺——Life in CCSU 一. 第二次Scrum任务 继续上一次冲刺的内容,这次完成论坛部分. 二. 用户故事 用户输入账号.密码: 用户点击论坛部分: 系统显示帖子: 用户选 ...

  5. 我在web前端路上的第一个脚印

    这是我的第一篇博客,希望记录下我在路上见到的风景,也和大家一起分享.

  6. JSP + JDBC + MySQL 读取数据库内容到网页

    创建数据库表 导入JDCB驱动 mysql.jsp <%@ page language="java" %> <%@ page contentType=" ...

  7. 第3章 Data语意学

    在C++中经常会遇到一个类的大小问题,关于一个类的大小一般受到三个方面的影响. 语言本身所造成的额外负担,如在虚拟继承中会遇到如派生类中会包含一个指针指向base class subobjec,这样会 ...

  8. admin-4

    每个学员机上有三台预先配置好的虚拟机server —— 作为练习用服务器desktop —— 作为练习用客户机classroom —— 提供网关/DNS/软件素材等资源 通过真机上“虚拟系统管理器”访 ...

  9. git 安装部署教程

    转转改改修修,能用:   查看用户名和邮箱 $ git config user.name $ git config user.email   修改用户名和邮箱 $ git config user.na ...

  10. windows7 64位系统安装CPU版本TensorFlow(anaconda3.6)

    1>下载anaconda3.6,https://www.anaconda.com/download/,选择64位的anaconda3.6,安装时候,路径可以自定义,但是要选择把路径添加到环境变量 ...