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

ASP.NET Core中将Json字符串转换为JsonResult
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" };