Server.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using System;
  2. using System.IO;
  3. #if NET45
  4. System.Web
  5. #else
  6. using Microsoft.AspNetCore.Http;
  7. #endif
  8. namespace Winsoft.GOV.XF.WebApi.WXCore.Utilities
  9. {
  10. public static class Server
  11. {
  12. private static string _appDomainAppPath;
  13. public static string AppDomainAppPath
  14. {
  15. get
  16. {
  17. if (_appDomainAppPath == null)
  18. {
  19. #if NET45
  20. _appDomainAppPath = HttpRuntime.AppDomainAppPath;
  21. #else
  22. _appDomainAppPath = AppContext.BaseDirectory; //dll所在目录:;
  23. #endif
  24. }
  25. return _appDomainAppPath;
  26. }
  27. set
  28. {
  29. _appDomainAppPath = value;
  30. #if NETSTANDARD1_6
  31. if (!_appDomainAppPath.EndsWith("\\"))
  32. {
  33. _appDomainAppPath += "\\";
  34. }
  35. #endif
  36. }
  37. }
  38. public static string GetMapPath(string virtualPath)
  39. {
  40. if (virtualPath == null)
  41. {
  42. return "";
  43. }
  44. else if (virtualPath.StartsWith("~/"))
  45. {
  46. return virtualPath.Replace("~/", AppDomainAppPath).Replace("/", "\\");
  47. }
  48. else
  49. {
  50. return Path.Combine(AppDomainAppPath, virtualPath.Replace("/", "\\"));
  51. }
  52. }
  53. public static HttpContext HttpContext
  54. {
  55. get
  56. {
  57. #if NET45
  58. HttpContext context = HttpContext.Current;
  59. if (context == null)
  60. {
  61. HttpRequest request = new HttpRequest("Default.aspx", "http://sdk.weixin.senparc.com/default.aspx", null);
  62. StringWriter sw = new StringWriter();
  63. HttpResponse response = new HttpResponse(sw);
  64. context = new HttpContext(request, response);
  65. }
  66. #else
  67. HttpContext context = new DefaultHttpContext();
  68. #endif
  69. return context;
  70. }
  71. }
  72. }
  73. }