.net6 访问静态文件 ContentType MIME

 private static void InjectResourcePath(WebApplication app, string path, string requestPath)
        {
            var provider = new FileExtensionContentTypeProvider();
            provider.Mappings[".prj"] = "application/octet-stream";
            provider.Mappings[".sgy"] = "application/octet-stream";

            //发布时服务器注册静态资源
            string fileUpload = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, path);
            if (!Directory.Exists(fileUpload))
            {
                Directory.CreateDirectory(fileUpload);
            }
            app.UseStaticFiles(new StaticFileOptions
            {
                FileProvider = new PhysicalFileProvider(fileUpload),
                RequestPath = requestPath,
                ContentTypeProvider = provider,
                //不识别的文件是否处理
                ServeUnknownFileTypes = true,
                //默认方式
                DefaultContentType = "application/octet-stream"
                //DefaultContentType = "application/x-msdownload"
            });
        }
posted @ 2024-11-11 18:34  Hey,Coder!  阅读(2)  评论(0编辑  收藏  举报