Server.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Web;
  7. namespace Senparc.Weixin.MP.Sample.CommonService.Utilities
  8. {
  9. public static class Server
  10. {
  11. private static string _appDomainAppPath;
  12. public static string AppDomainAppPath
  13. {
  14. get
  15. {
  16. if (_appDomainAppPath == null)
  17. {
  18. _appDomainAppPath = HttpRuntime.AppDomainAppPath;
  19. }
  20. return _appDomainAppPath;
  21. }
  22. set
  23. {
  24. _appDomainAppPath = value;
  25. }
  26. }
  27. public static string GetMapPath(string virtualPath)
  28. {
  29. if (virtualPath == null)
  30. {
  31. return "";
  32. }
  33. else if (virtualPath.StartsWith("~/"))
  34. {
  35. return virtualPath.Replace("~/", AppDomainAppPath).Replace("/", "\\");
  36. }
  37. else
  38. {
  39. return Path.Combine(AppDomainAppPath, virtualPath.Replace("/", "\\"));
  40. }
  41. }
  42. public static HttpContext HttpContext
  43. {
  44. get
  45. {
  46. HttpContext context = HttpContext.Current;
  47. if (context == null)
  48. {
  49. HttpRequest request = new HttpRequest("Default.aspx", "http://weixin.senparc.com/default.aspx", null);
  50. StringWriter sw = new StringWriter();
  51. HttpResponse response = new HttpResponse(sw);
  52. context = new HttpContext(request, response);
  53. }
  54. return context;
  55. }
  56. }
  57. }
  58. }