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