开发者

Can I convert a JSON string into JsonResult?

开发者 https://www.devze.com 2022-12-28 04:43 出处:网络
I have some stored JSON strings stored in the DB which I want to return to the client as JsonResult . I know that Json(object) turns an obje开发者_StackOverflow社区ct into JsonResult but what if I alr

I have some stored JSON strings stored in the DB which I want to return to the client as JsonResult . I know that Json(object) turns an obje开发者_StackOverflow社区ct into JsonResult but what if I already have the result in a string ? can I cast it to JsonResult


You don't need to return a JsonResult because its job is to serialize an object into JSON string. You already have the JSON string, so just return it in a ContentResult and specify the correct content type:

string json = //get some json from your DB
return new ContentResult { Content = json, ContentType = "application/json" };

Remember that your MVC action methods should all have ActionResult as a return type, so you can return ContentResult just as easily as JsonResult.

0

精彩评论

暂无评论...
验证码 换一张
取 消