StaticFilesConfig.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. #region rootPath
  17. private string _rootPath = null;
  18. /// <summary>
  19. /// 静态文件路径。可为相对路径或绝对路径。若为空或空字符串则为默认路径(wwwroot)。demo:"wwwroot"
  20. /// </summary>
  21. [JsonProperty]
  22. public string rootPath
  23. {
  24. get => _rootPath;
  25. set
  26. {
  27. if (string.IsNullOrEmpty(value))
  28. {
  29. _rootPath = null;
  30. return;
  31. }
  32. else
  33. {
  34. _rootPath = CommonHelp.GetAbsPath(value);
  35. }
  36. }
  37. }
  38. #endregion
  39. /// <summary>
  40. /// 默认页面(可不指定)。An ordered list of file names to select by default. List length and ordering may affect performance
  41. /// </summary>
  42. [JsonProperty]
  43. public List<string> defaultFileNames { get; set; }
  44. /// <summary>
  45. /// 是否可浏览目录(default false)。Enables directory browsing
  46. /// </summary>
  47. [JsonProperty]
  48. public bool? useDirectoryBrowser { get; set; }
  49. /// <summary>
  50. /// 回应静态文件时额外添加的http回应头。可不指定。
  51. /// </summary>
  52. [JsonProperty]
  53. public IDictionary<string,string> responseHeaders { get; set; }
  54. #region contentTypeProvider
  55. internal IContentTypeProvider contentTypeProvider { get; set; }
  56. /// <summary>
  57. /// 静态文件类型映射配置的文件路径。可为相对路径或绝对路径。例如"contentTypeMap.json"。若不指定(或指定的文件不存在)则不指定文件类型映射配置
  58. /// </summary>
  59. [JsonProperty]
  60. public string contentTypeMapFile
  61. {
  62. set
  63. {
  64. if (string.IsNullOrWhiteSpace(value))
  65. {
  66. contentTypeProvider = null;
  67. return;
  68. }
  69. var jsonFile = new JsonFile(value);
  70. if (File.Exists(jsonFile.configPath))
  71. {
  72. var provider = new FileExtensionContentTypeProvider();
  73. contentTypeProvider = provider;
  74. if (jsonFile.root is JObject jo)
  75. {
  76. var map = provider.Mappings;
  77. foreach (var item in jo)
  78. {
  79. map.Remove(item.Key);
  80. map[item.Key] = item.Value.Value<string>();
  81. }
  82. }
  83. }
  84. }
  85. }
  86. #endregion
  87. /// <summary>
  88. /// 初始化 wwwroot静态文件配置的操作
  89. /// </summary>
  90. public Action<StaticFileOptions> OnInitStaticFileOptions { get; set; }
  91. }
  92. #endregion
  93. }