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