using System; using Microsoft.AspNetCore.Builder; using System.IO; using Vit.Core.Util.Common; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System.Collections.Generic; using Microsoft.AspNetCore.StaticFiles; using Vit.Core.Util.ConfigurationManager; namespace Vit.WebHost { #region StaticFiles [JsonObject(MemberSerialization.OptIn)] public class StaticFilesConfig { #region rootPath private string _rootPath = null; /// /// 静态文件路径。可为相对路径或绝对路径。若为空或空字符串则为默认路径(wwwroot)。demo:"wwwroot" /// [JsonProperty] public string rootPath { get => _rootPath; set { if (string.IsNullOrEmpty(value)) { _rootPath = null; return; } else { _rootPath = CommonHelp.GetAbsPath(value); } } } #endregion /// /// 默认页面(可不指定)。An ordered list of file names to select by default. List length and ordering may affect performance /// [JsonProperty] public List defaultFileNames { get; set; } /// /// 是否可浏览目录(default false)。Enables directory browsing /// [JsonProperty] public bool? useDirectoryBrowser { get; set; } /// /// 回应静态文件时额外添加的http回应头。可不指定。 /// [JsonProperty] public IDictionary responseHeaders { get; set; } #region contentTypeProvider internal IContentTypeProvider contentTypeProvider { get; set; } /// /// 静态文件类型映射配置的文件路径。可为相对路径或绝对路径。例如"contentTypeMap.json"。若不指定(或指定的文件不存在)则不指定文件类型映射配置 /// [JsonProperty] public string contentTypeMapFile { set { if (string.IsNullOrWhiteSpace(value)) { contentTypeProvider = null; return; } var jsonFile = new JsonFile(value); if (File.Exists(jsonFile.configPath)) { var provider = new FileExtensionContentTypeProvider(); contentTypeProvider = provider; if (jsonFile.root is JObject jo) { var map = provider.Mappings; foreach (var item in jo) { map.Remove(item.Key); map[item.Key] = item.Value.Value(); } } } } } #endregion /// /// 初始化 wwwroot静态文件配置的操作 /// public Action OnInitStaticFileOptions { get; set; } } #endregion }