Handler.cs 1.2 KB

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