You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

25 lines
721 B

2 years ago
  1. using System;
  2. using System.Net.Http;
  3. using System.Text;
  4. using System.Web.Script.Serialization;
  5. namespace EasyBL.WebApi.Common
  6. {
  7. public class HttpResponseExtension
  8. {
  9. public static HttpResponseMessage ToJson(Object obj)
  10. {
  11. String str;
  12. if (obj is String || obj is Char)
  13. {
  14. str = obj.ToString();
  15. }
  16. else
  17. {
  18. var serializer = new JavaScriptSerializer();
  19. str = serializer.Serialize(obj);
  20. }
  21. var result = new HttpResponseMessage { Content = new StringContent(str, Encoding.GetEncoding("UTF-8"), "application/json") };
  22. return result;
  23. }
  24. }
  25. }