GlobalContext.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. using System;
  2. using System.Reflection;
  3. using System.Text;
  4. using Microsoft.AspNetCore.Hosting;
  5. using Microsoft.Extensions.Configuration;
  6. using Microsoft.Extensions.DependencyInjection;
  7. using Microsoft.AspNetCore.StaticFiles;
  8. using Microsoft.Extensions.Hosting;
  9. using YiSha.Util.Model;
  10. using Microsoft.AspNetCore.Http;
  11. namespace YiSha.Util
  12. {
  13. public class GlobalContext
  14. {
  15. /// <summary>
  16. /// All registered service and class instance container. Which are used for dependency injection.
  17. /// </summary>
  18. public static IServiceCollection Services { get; set; }
  19. /// <summary>
  20. /// Configured service provider.
  21. /// </summary>
  22. public static IServiceProvider ServiceProvider { get; set; }
  23. public static IConfiguration Configuration { get; set; }
  24. public static IWebHostEnvironment HostingEnvironment { get; set; }
  25. public static SystemConfig SystemConfig { get; set; }
  26. public static string GetVersion()
  27. {
  28. Version version = Assembly.GetEntryAssembly().GetName().Version;
  29. return version.Major + "." + version.Minor;
  30. }
  31. /// <summary>
  32. /// 程序启动时,记录目录
  33. /// </summary>
  34. /// <param name="env"></param>
  35. public static void LogWhenStart(IWebHostEnvironment env)
  36. {
  37. StringBuilder sb = new StringBuilder();
  38. sb.Append("程序启动");
  39. sb.Append(" |ContentRootPath:" + env.ContentRootPath);
  40. sb.Append(" |WebRootPath:" + env.WebRootPath);
  41. sb.Append(" |IsDevelopment:" + env.IsDevelopment());
  42. LogHelper.Debug(sb.ToString());
  43. }
  44. /// <summary>
  45. /// 设置cache control
  46. /// </summary>
  47. /// <param name="context"></param>
  48. public static void SetCacheControl(StaticFileResponseContext context)
  49. {
  50. int second = 365 * 24 * 60 * 60;
  51. context.Context.Response.Headers.Add("Cache-Control", new[] { "public,max-age=" + second });
  52. context.Context.Response.Headers.Add("Expires", new[] { DateTime.UtcNow.AddYears(1).ToString("R") }); // Format RFC1123
  53. }
  54. }
  55. }