使用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. python 将汉字转换为拼音

    xpinyin提供把汉字转为汉语拼音的功能. 安装此模块 pip install xpinyin简单用例: from xpinyin import Pinyin pin = Pinyin() test ...

  2. thinkphp5.0引入类

    /application/index/controller/Test.php <?php namespace app\index\controller; 当前命名空间名称 use think\C ...

  3. python 8

    一.文件操作初识 1. path 文件路径 F:\文件.txt encoding 编码方式 utf-8, gbk ... mode 操作方式 只读,只写,读写,写读,追加... f1 = open(r ...

  4. JAVA第九次作业

    JAVA第九次作业 (一)学习总结 1.用思维导图对javaIO操作的学习内容进行总结. 参考资料: XMind. 2.下面的程序实现了文件的拷贝,但采用的是一个字节一个字节的读写方式,效率很低.使用 ...

  5. Azure monitor Portal 、Azure monitor API监控指标、性能监视器常用指标

  6. 访问Mat某一个像素值

    mat.at<uchar>(row, col): 如果想要用cout打印出来,前面要加上(int),否则打印出来的是空字符:

  7. loj#2720. 「NOI2018」你的名字

    链接大合集: loj uoj luogu bzoj 单纯地纪念一下写的第一份5K代码.../躺尸 因为ZJOI都不会所以只好写NOI的题了... 总之字符串题肯定一上来就拼个大字符串跑后缀数组啦! ( ...

  8. 如何写更少的 if else

    首先声明,不是要消除if 而是,减少一些不必要的if判断,使得代码更优雅,更重要的是提高可维护性 most easy use Ternary: var result = condiction? tru ...

  9. [SQL] 从文本中提取数值

    现需求从上方测试数据的“备注”列中提取出金额 目前有两个方法比较容易实现: 1.首先比较容易想到的就是利用函数stuff删除掉所有的非数值字符. STUFF ( character_expressio ...

  10. 旁路、去耦、Bulk以及耦合电容的作用与区别

    在硬件设计中有很多种电容,各种电容的功能.种类和电容容值各不相同.按照功能划分的话,最重要的几种电容分别称为:去耦电容(De-coupling Capacitor),旁路电容(Bypass Capac ...