StaticFilesConfig.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. using System;
  2. using Microsoft.AspNetCore.Builder;
  3. using System.IO;
  4. using Vit.Core.Util.Common;
  5. using Newtonsoft.Json;
  6. using Newtonsoft.Json.Linq;
  7. using System.Collections.Generic;
  8. using Microsoft.AspNetCore.StaticFiles;
  9. using Vit.Core.Util.ConfigurationManager;
  10. namespace Vit.WebHost
  11. {
  12. #region StaticFiles
  13. [JsonObject(MemberSerialization.OptIn)]
  14. public class StaticFilesConfig
  15. {
  16. /// <summary>
  17. /// 请求路径(可不指定)。demo:"/file/static"。The relative request path that maps to static resources
  18. /// </summary>
  19. [JsonProperty]
  20. public string requestPath { get; set; }
  21. #region rootPath
  22. private string _rootPath = null;
  23. /// <summary>
  24. /// 静态文件路径。可为相对路径或绝对路径。若为空或空字符串则为默认路径(wwwroot)。demo:"wwwroot"
  25. /// </summary>
  26. [JsonProperty]
  27. public string rootPath
  28. {
  29. get => _rootPath;
  30. set
  31. {
  32. if (string.IsNullOrEmpty(value))
  33. {
  34. _rootPath = null;
  35. return;
  36. }
  37. else
  38. {
  39. _rootPath = CommonHelp.GetAbsPath(value);
  40. }
  41. }
  42. }
  43. #endregion
  44. /// <summary>
  45. /// 默认页面(可不指定)。An ordered list of file names to select by default. List length and ordering may affect performance
  46. /// </summary>
  47. [JsonProperty]
  48. public List<string> defaultFileNames { get; set; }
  49. /// <summary>
  50. /// 是否可浏览目录(default false)。Enables directory browsing
  51. /// </summary>
  52. [JsonProperty]
  53. public bool? useDirectoryBrowser { get; set; }
  54. /// <summary>
  55. /// 回应静态文件时额外添加的http回应头。可不指定。
  56. /// </summary>
  57. [JsonProperty]
  58. public IDictionary<string, string> responseHeaders { get; set; }
  59. #region contentTypeProvider
  60. internal IContentTypeProvider contentTypeProvider { get; set; }
  61. /// <summary>
  62. /// 静态文件类型映射配置的文件路径。可为相对路径或绝对路径。例如"contentTypeMap.json"。若不指定(或指定的文件不存在)则不指定文件类型映射配置
  63. /// </summary>
  64. [JsonProperty]
  65. public string contentTypeMapFile
  66. {
  67. set
  68. {
  69. if (string.IsNullOrWhiteSpace(value))
  70. {
  71. contentTypeProvider = null;
  72. return;
  73. }
  74. var jsonFile = new JsonFile(value);
  75. if (File.Exists(jsonFile.configPath))
  76. {
  77. var provider = new FileExtensionContentTypeProvider();
  78. contentTypeProvider = provider;
  79. if (jsonFile.root is JObject jo)
  80. {
  81. var map = provider.Mappings;
  82. foreach (var item in jo)
  83. {
  84. map.Remove(item.Key);
  85. map[item.Key] = item.Value.Value<string>();
  86. }
  87. }
  88. }
  89. }
  90. }
  91. #endregion
  92. /// <summary>
  93. /// 初始化 wwwroot静态文件配置的操作
  94. /// </summary>
  95. public Action<StaticFileOptions> OnInitStaticFileOptions { get; set; }
  96. }
  97. #endregion
  98. }