zoukankan      html  css  js  c++  java
  • newtonsoft返回json去掉字符串

    在ASP.NET MVC中使用newtonsoft转义的字符串默认加了转义,很烦人,解决方案:

    在webapi中不要返回string类型,使用HttpResponseMessage

    [HttpGet]
    public HttpResponseMessage GetQustions(int page=1,int limit=10)
    {
        LayuiTableData result = new LayuiTableData
         {
            code = 0,
            msg = "",
            count = _context.Questions.Count(),
            data = _context.Questions.ToList()
        };
        return new HttpResponseMessage { Content = new StringContent(result.ToJson(),         Encoding.GetEncoding("UTF-8")) };
        //return result.ToJson();
        //return Common.Json.ToJson(result, "yyyy-MM-dd");
    }

    在mvc中不要返回string类型,返回Json,并且设置Response的ContentType:

     // 获取所有用户
    public JsonResult List()
    {
        var data = userApp.GetAll();
        var result = new
        {
            total=2,
            rows=data,
            code=0,
            msg=0
        };
        Response.ContentType = "application/json";
        return Json(result, JsonRequestBehavior.AllowGet);
    }
  • 相关阅读:
    LeetCode之移除元素
    有被开心到hh(日常)
    交换排序
    插入排序
    顺序查找&折半查找
    C++之引用
    MySQL学习笔记
    C/C++程序编译过程
    计算机面试知识整合(更新中...)
    MFC之编辑框
  • 原文地址:https://www.cnblogs.com/AlexanderZhao/p/12878908.html
Copyright © 2011-2022 走看看