IApplicationBuilderExtensions_UseStaticFiles.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. using Microsoft.AspNetCore.Builder;
  2. using Microsoft.Extensions.FileProviders;
  3. using System.IO;
  4. using Vit.Core.Util.Common;
  5. using Vit.WebHost;
  6. namespace Vit.Extensions
  7. {
  8. public static class IApplicationBuilderExtensions_UseStaticFiles
  9. {
  10. /// <summary>
  11. /// 启用静态文件服务
  12. ///
  13. /// <example>
  14. /// <code>
  15. /// /* 映射静态文件。若不指定则不映射静态文件 */
  16. /// "staticFiles": {
  17. ///
  18. /// /* 请求路径(可不指定)。demo:"/file/static"。The relative request path that maps to static resources */
  19. /// //"requestPath": "/file",
  20. ///
  21. /// /* 静态文件路径。可为相对路径或绝对路径。若为空或空字符串则为默认路径(wwwroot)。demo:"wwwroot/demo" */
  22. /// //"rootPath": "wwwroot",
  23. ///
  24. /// /* 默认页面(可不指定)。An ordered list of file names to select by default. List length and ordering may affect performance */
  25. /// //"defaultFileNames": [],
  26. ///
  27. /// /* 是否可浏览目录(default false)。Enables directory browsing */
  28. /// //"useDirectoryBrowser": false,
  29. ///
  30. /// /* 静态文件类型映射配置的文件路径。可为相对路径或绝对路径。例如"contentTypeMap.json"。若不指定(或指定的文件不存在)则不指定文件类型映射配置 */
  31. /// "contentTypeMapFile": "contentTypeMap.json",
  32. ///
  33. /// /* 回应静态文件时额外添加的http回应头。可不指定。 */
  34. /// "responseHeaders": {
  35. ///
  36. /// //设置浏览器静态文件缓存3600秒
  37. /// "Cache-Control": "public,max-age=3600"
  38. /// }
  39. /// }
  40. ///
  41. /// </code>
  42. /// </example>
  43. ///
  44. /// </summary>
  45. /// <param name="data"></param>
  46. /// <param name="configPath">在appsettings.json文件中的路径。默认:"server.staticFiles"。</param>
  47. /// <returns></returns>
  48. public static IApplicationBuilder UseStaticFilesFromConfig(this IApplicationBuilder data, string configPath = "server.staticFiles")
  49. {
  50. return data.UseStaticFiles(Vit.Core.Util.ConfigurationManager.ConfigurationManager.Instance.GetByPath<Vit.WebHost.StaticFilesConfig>(configPath));
  51. }
  52. /// <summary>
  53. /// 启用静态文件服务
  54. ///
  55. /// <example>
  56. /// <code>
  57. /// /* 映射静态文件。若不指定则不映射静态文件 */
  58. /// "staticFiles": {
  59. ///
  60. /// /* 请求路径(可不指定)。demo:"/file/static"。The relative request path that maps to static resources */
  61. /// //"requestPath": "/file",
  62. ///
  63. /// /* 静态文件路径。可为相对路径或绝对路径。若为空或空字符串则为默认路径(wwwroot)。demo:"wwwroot/demo" */
  64. /// //"rootPath": "wwwroot",
  65. ///
  66. /// /* 默认页面(可不指定)。An ordered list of file names to select by default. List length and ordering may affect performance */
  67. /// //"defaultFileNames": [],
  68. ///
  69. /// /* 是否可浏览目录(default false)。Enables directory browsing */
  70. /// //"useDirectoryBrowser": false,
  71. ///
  72. /// /* 静态文件类型映射配置的文件路径。可为相对路径或绝对路径。例如"contentTypeMap.json"。若不指定(或指定的文件不存在)则不指定文件类型映射配置 */
  73. /// "contentTypeMapFile": "contentTypeMap.json",
  74. ///
  75. /// /* 回应静态文件时额外添加的http回应头。可不指定。 */
  76. /// "responseHeaders": {
  77. ///
  78. /// //设置浏览器静态文件缓存3600秒
  79. /// "Cache-Control": "public,max-age=3600"
  80. /// }
  81. /// }
  82. ///
  83. /// </code>
  84. /// </example>
  85. ///
  86. /// </summary>
  87. /// <param name="data"></param>
  88. /// <param name="config"></param>
  89. /// <returns></returns>
  90. public static IApplicationBuilder UseStaticFiles(this IApplicationBuilder data, StaticFilesConfig config)
  91. {
  92. if (config == null || data == null) return data;
  93. #region (x.1)UseStaticFiles
  94. var staticfileOptions = new StaticFileOptions();
  95. //(x.x.1)requestPath
  96. if (!string.IsNullOrEmpty(config.requestPath))
  97. {
  98. staticfileOptions.RequestPath = new Microsoft.AspNetCore.Http.PathString(config.requestPath);
  99. }
  100. #region (x.x.2)FileProvider
  101. IFileProvider fileProvider=null;
  102. string rootPath;
  103. if (!string.IsNullOrWhiteSpace(rootPath=config.rootPath) && Directory.Exists(rootPath))
  104. {
  105. fileProvider = new PhysicalFileProvider(rootPath);
  106. }
  107. else if (Directory.Exists(rootPath = CommonHelp.GetAbsPath("wwwroot")))
  108. {
  109. fileProvider = new PhysicalFileProvider(rootPath);
  110. }
  111. else
  112. {
  113. var dir = new DirectoryInfo("wwwroot");
  114. if(dir.Exists)
  115. fileProvider = new PhysicalFileProvider(dir.FullName);
  116. }
  117. staticfileOptions.FileProvider = fileProvider;
  118. #endregion
  119. //(x.x.3)OnInitStaticFileOptions
  120. config.OnInitStaticFileOptions?.Invoke(staticfileOptions);
  121. #region (x.x.4)Response Headers
  122. if (config.responseHeaders != null)
  123. {
  124. staticfileOptions.OnPrepareResponse +=
  125. ctx =>
  126. {
  127. var Headers = ctx.Context.Response.Headers;
  128. foreach (var kv in config.responseHeaders)
  129. {
  130. Headers[kv.Key] = kv.Value;
  131. }
  132. };
  133. }
  134. #endregion
  135. //(x.x.5)contentTypeProvider
  136. if (config.contentTypeProvider != null)
  137. {
  138. staticfileOptions.ContentTypeProvider = config.contentTypeProvider;
  139. }
  140. //(x.x.6)UseDefaultFiles
  141. if (config.defaultFileNames != null)
  142. {
  143. data.UseDefaultFiles(new DefaultFilesOptions
  144. {
  145. DefaultFileNames = config.defaultFileNames,
  146. FileProvider= fileProvider
  147. });
  148. }
  149. //(x.x.7)UseStaticFiles
  150. data.UseStaticFiles(staticfileOptions);
  151. #endregion
  152. //(x.2)UseDirectoryBrowser
  153. if (config.useDirectoryBrowser == true)
  154. {
  155. var options = new DirectoryBrowserOptions {
  156. RequestPath = staticfileOptions.RequestPath,
  157. FileProvider = fileProvider
  158. };
  159. data.UseDirectoryBrowser(options);
  160. }
  161. return data;
  162. }
  163. }
  164. }