What is better to return from the controller - a JsonResult or a string?
https://softwareengineering.stackexchange.com/questions/291797
-
09-10-2020 - |
سؤال
I have a WebAPI application and I have a controller method which is used to return a json result to the UI.
Now, I can either return a string (because the json is basically a string - and it works properly), or I can return a JsonResult from System.Web.Http.Results.
Are there any advantages to using the JsonResult?
المحلول
It depends. How is the result meant or expected to be used?
If it should be a string (i.e. no associated json semantics), then return it as a string.
If you wish to use of the facilities associated with the json data, then keep the JsonResult
.
If you are unsure, keep the JsonResult
and let the client convert it if they require it to be just a string.