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);
}

 

posted @ 2019-09-15 18:02  星空天宇  阅读(175)  评论(0编辑  收藏  举报