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


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM