IWebHostBuilderExtensions_UseStaticFiles.cs 4.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. using Microsoft.AspNetCore.Hosting;
  2. using Microsoft.Extensions.DependencyInjection;
  3. using Vit.WebHost;
  4. namespace Vit.Extensions
  5. {
  6. public static partial class IWebHostBuilderExtensions_UseStaticFiles
  7. {
  8. /// <summary>
  9. /// 启用静态文件服务
  10. ///
  11. /// <example>
  12. /// <code>
  13. /// /* 映射静态文件。若不指定则不映射静态文件 */
  14. /// "staticFiles": {
  15. ///
  16. /// /* 请求路径(可不指定)。demo:"/file/static"。The relative request path that maps to static resources */
  17. /// //"requestPath": "/file",
  18. ///
  19. /// /* 静态文件路径。可为相对路径或绝对路径。若为空或空字符串则为默认路径(wwwroot)。demo:"wwwroot/demo" */
  20. /// //"rootPath": "wwwroot",
  21. ///
  22. /// /* 默认页面(可不指定)。An ordered list of file names to select by default. List length and ordering may affect performance */
  23. /// //"defaultFileNames": [],
  24. ///
  25. /// /* 是否可浏览目录(default false)。Enables directory browsing */
  26. /// //"useDirectoryBrowser": false,
  27. ///
  28. /// /* 静态文件类型映射配置的文件路径。可为相对路径或绝对路径。例如"contentTypeMap.json"。若不指定(或指定的文件不存在)则不指定文件类型映射配置 */
  29. /// "contentTypeMapFile": "contentTypeMap.json",
  30. ///
  31. /// /* 回应静态文件时额外添加的http回应头。可不指定。 */
  32. /// "responseHeaders": {
  33. ///
  34. /// //设置浏览器静态文件缓存3600秒
  35. /// "Cache-Control": "public,max-age=3600"
  36. /// }
  37. /// }
  38. ///
  39. /// </code>
  40. /// </example>
  41. ///
  42. /// </summary>
  43. /// <param name="data"></param>
  44. /// <param name="configPath">在appsettings.json文件中的路径。默认:"server.staticFiles"。</param>
  45. /// <returns></returns>
  46. public static IWebHostBuilder UseStaticFilesFromConfig(this IWebHostBuilder data, string configPath = "server.staticFiles")
  47. {
  48. return data.UseStaticFiles(Vit.Core.Util.ConfigurationManager.ConfigurationManager.Instance.GetByPath<Vit.WebHost.StaticFilesConfig>(configPath));
  49. }
  50. /// <summary>
  51. /// 启用静态文件服务
  52. ///
  53. /// <example>
  54. /// <code>
  55. /// /* 映射静态文件。若不指定则不映射静态文件 */
  56. /// "staticFiles": {
  57. ///
  58. /// /* 请求路径(可不指定)。demo:"/file/static"。The relative request path that maps to static resources */
  59. /// //"requestPath": "/file",
  60. ///
  61. /// /* 静态文件路径。可为相对路径或绝对路径。若为空或空字符串则为默认路径(wwwroot)。demo:"wwwroot/demo" */
  62. /// //"rootPath": "wwwroot",
  63. ///
  64. /// /* 默认页面(可不指定)。An ordered list of file names to select by default. List length and ordering may affect performance */
  65. /// //"defaultFileNames": [],
  66. ///
  67. /// /* 是否可浏览目录(default false)。Enables directory browsing */
  68. /// //"useDirectoryBrowser": false,
  69. ///
  70. /// /* 静态文件类型映射配置的文件路径。可为相对路径或绝对路径。例如"contentTypeMap.json"。若不指定(或指定的文件不存在)则不指定文件类型映射配置 */
  71. /// "contentTypeMapFile": "contentTypeMap.json",
  72. ///
  73. /// /* 回应静态文件时额外添加的http回应头。可不指定。 */
  74. /// "responseHeaders": {
  75. ///
  76. /// //设置浏览器静态文件缓存3600秒
  77. /// "Cache-Control": "public,max-age=3600"
  78. /// }
  79. /// }
  80. ///
  81. /// </code>
  82. /// </example>
  83. ///
  84. /// </summary>
  85. /// <param name="data"></param>
  86. /// <param name="config"></param>
  87. /// <returns></returns>
  88. public static IWebHostBuilder UseStaticFiles(this IWebHostBuilder data, StaticFilesConfig config)
  89. {
  90. data?.Configure(app=> app.UseStaticFiles(config));
  91. return data;
  92. }
  93. }
  94. }