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.

40 lines
1.1 KiB

2 years ago
  1. using Newtonsoft.Json;
  2. using System;
  3. using System.Web;
  4. /// <summary>
  5. /// Handler 的摘要说明
  6. /// </summary>
  7. public abstract class Handler
  8. {
  9. protected Handler(HttpContext context)
  10. {
  11. this.Request = context.Request;
  12. this.Response = context.Response;
  13. this.Context = context;
  14. this.Server = context.Server;
  15. }
  16. public abstract void Process();
  17. protected void WriteJson(object response)
  18. {
  19. var jsonpCallback = Request["callback"];
  20. var json = JsonConvert.SerializeObject(response);
  21. if (String.IsNullOrWhiteSpace(jsonpCallback))
  22. {
  23. Response.AddHeader("Content-Type", "text/plain");
  24. Response.Write(json);
  25. }
  26. else
  27. {
  28. Response.AddHeader("Content-Type", "application/javascript");
  29. Response.Write($"{jsonpCallback}({json});");
  30. }
  31. Response.End();
  32. }
  33. public HttpRequest Request { get; private set; }
  34. public HttpResponse Response { get; private set; }
  35. public HttpContext Context { get; private set; }
  36. public HttpServerUtility Server { get; private set; }
  37. }