WebHostHelp.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. using Microsoft.AspNetCore.StaticFiles;
  2. using Newtonsoft.Json.Linq;
  3. using System.IO;
  4. using Vit.Core.Util.ConfigurationManager;
  5. namespace Vit.WebHost
  6. {
  7. public class WebHostHelp
  8. {
  9. #region BuildContentTypeProvider
  10. /// <summary>
  11. /// 静态文件类型映射配置的文件路径。可为相对路径或绝对路径。例如"contentTypeMap.json"。若不指定(或指定的文件不存在)则返回null
  12. /// </summary>
  13. /// <param name="contentTypeMapFile"></param>
  14. /// <returns></returns>
  15. public static FileExtensionContentTypeProvider BuildContentTypeProvider(string contentTypeMapFile)
  16. {
  17. if (string.IsNullOrWhiteSpace(contentTypeMapFile))
  18. {
  19. return null;
  20. }
  21. var jsonFile = new JsonFile(contentTypeMapFile);
  22. if (File.Exists(jsonFile.configPath))
  23. {
  24. var provider = new FileExtensionContentTypeProvider();
  25. if (jsonFile.root is JObject jo)
  26. {
  27. var map = provider.Mappings;
  28. foreach (var item in jo)
  29. {
  30. map.Remove(item.Key);
  31. map[item.Key] = item.Value.Value<string>();
  32. }
  33. }
  34. return provider;
  35. }
  36. return null;
  37. }
  38. #endregion
  39. }
  40. }