سؤال

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى softwareengineering.stackexchange
scroll top