ASP.NET Core中将Json字符串转换为JsonResult

时间:2020-12-23 19:13:33   收藏:0   阅读:257

ASP.NET Core中返回JsonResult

最近在使用NET 5.0做WebApi中,发现只能返回string类型,不能用JsonResult返回实体,于是查阅资料找到解决办法。
两种方式分别为:

1.返回string类型

return new JsonResult(string)

2.JsonResult输出实体

    //此处需要引用Newtonsoft.Json,在NuGet中下载
    string jsonStr = JsonConvert.SerializeObject(classOutJson);
    return new ContentResult { Content = jsonStr, ContentType = "application/json" };

原文:https://www.cnblogs.com/Drock/p/14179505.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!