From 4b80b16ca60ff4c660b8a77683292a7e1a0786e1 Mon Sep 17 00:00:00 2001 From: yzp Date: Thu, 14 Nov 2024 21:13:58 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BC=80=E6=94=BE=E5=85=A8=E9=83=A8=E9=9D=99?= =?UTF-8?q?=E6=80=81=E8=B5=84=E6=BA=90=E7=9A=84=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Configuration/App.json | 3 +- Admin.NET/Admin.NET.Web.Core/Startup.cs | 48 ++++++++++++++----- 2 files changed, 38 insertions(+), 13 deletions(-) diff --git a/Admin.NET/Admin.NET.Application/Configuration/App.json b/Admin.NET/Admin.NET.Application/Configuration/App.json index 3ae8754c..917d3b3c 100644 --- a/Admin.NET/Admin.NET.Application/Configuration/App.json +++ b/Admin.NET/Admin.NET.Application/Configuration/App.json @@ -21,8 +21,9 @@ "DefaultErrorMessage": "系统异常,请联系管理员", "ThrowBah": true, // 是否将 Oops.Oh 默认抛出为业务异常 "LogError": false // 是否输出异常日志 - }, + }, // 静态资源处理方式(允许这些文件被访问) + // 包含".*": "application/octet-stream"允许访问所有静态资源 "StaticContentTypeMappings": { ".dll": "application/octet-stream", ".exe": "application/octet-stream", diff --git a/Admin.NET/Admin.NET.Web.Core/Startup.cs b/Admin.NET/Admin.NET.Web.Core/Startup.cs index fcba80ef..fb274828 100644 --- a/Admin.NET/Admin.NET.Web.Core/Startup.cs +++ b/Admin.NET/Admin.NET.Web.Core/Startup.cs @@ -20,12 +20,14 @@ using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.HttpOverrides; using Microsoft.AspNetCore.ResponseCompression; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.FileProviders; using Microsoft.Extensions.Hosting; using Newtonsoft.Json; using OnceMi.AspNetCore.OSS; using SixLabors.ImageSharp.Web.DependencyInjection; using System; using System.Collections.Generic; +using System.IO; using System.Linq; using System.Text.Encodings.Web; using System.Text.Unicode; @@ -278,20 +280,42 @@ public class Startup : AppStartup var cpMappings = App.GetConfig>("StaticContentTypeMappings"); if (cpMappings != null) { - foreach (var key in cpMappings.Keys) + if (cpMappings.Keys.Contains(".*")) { - contentTypeProvider.Mappings[key] = cpMappings[key]; + app.UseStaticFiles(new StaticFileOptions + { + FileProvider = new PhysicalFileProvider(Path.Combine(env.ContentRootPath, "wwwroot")), + //RequestPath = "/static", + ServeUnknownFileTypes = true, // 允许服务未知文件类型,以便能处理.dll这种非默认的静态文件类型 + //DefaultContentType = "application/octet-stream" // 为未知文件类型设置一个通用的内容类型 + DefaultContentType = cpMappings[".*"] + }); } - } - app.UseStaticFiles(new StaticFileOptions - { - ContentTypeProvider = contentTypeProvider - }); - - //// 启用HTTPS - //app.UseHttpsRedirection(); - - // 启用OAuth + else + { + foreach (var key in cpMappings.Keys) + { + contentTypeProvider.Mappings[key] = cpMappings[key]; + } + app.UseStaticFiles(new StaticFileOptions + { + ContentTypeProvider = contentTypeProvider + }); + } + } + else + { + app.UseStaticFiles(new StaticFileOptions + { + ContentTypeProvider = contentTypeProvider + }); + } + + + //// 启用HTTPS + //app.UseHttpsRedirection(); + + // 启用OAuth app.UseOAuth(); // 添加状态码拦截中间件