.net core 静态文件中间件 电脑版发表于:2020/7/31 14:26 ![.netcore](https://img.tnblog.net/arcimg/hb/c857299a86d84ee7b26d181a31e58234.jpg ".netcore") >#.net core 静态文件中间件 [TOC] 静态文件中间件的能力 ------------ - 支持指定相对路径 - 支持目录浏览 - 支持设置默认文档 - 支持多目录映射 目录结构 ------------ ![](https://img.tnblog.net/arcimg/hb/6c40ff8cd8514dd5b60fe946c39d957d.jpg) 支持指定相对路径的访问 ------------ ><font style="color:#2ecc71;font-weight:bold;">Startup.cs</font> ```csharp public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseStaticFiles(); app.UseRouting(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); } ``` tn> `app.UseStaticFiles();`使得 web root(默认为wwwroot)下的文件目录可以被访问。随后将展示如何通过使用 `UseStaticFiles` 将其他目录下的内容也向外提供服务。 <br/> >运行测试 ![](https://img.tnblog.net/arcimg/hb/a80d863397d54a868bc8f7654d541b01.png) ![](https://img.tnblog.net/arcimg/hb/f3fb11d9de79442d9c40c4bbbc23a087.png) >默认访问静态文件的index.html ```csharp app.UseDefaultFiles(); ``` >运行测试 ![](https://img.tnblog.net/arcimg/hb/c2421d3c89714318a3ed9a8eb9c714a4.png) tn>一般默认文件的检索有<br/>default.htm<br/>default.html<br/>index.htm<br/>index.html<br/>可以通过以下代码达到自定义默认路径的效果。 ```csharp DefaultFilesOptions options = new DefaultFilesOptions(); options.DefaultFileNames.Clear(); //假设我们以 mydefault.html 为检索的对象 options.DefaultFileNames.Add("mydefault.html"); app.UseDefaultFiles(options); ``` 浏览目录 ------------ >添加目录服务 ```csharp services.AddDirectoryBrowser(); ``` >启用网络应用目录浏览 ```csharp app.UseDirectoryBrowser(); ``` >运行测试 ![](https://img.tnblog.net/arcimg/hb/ec8219117553496085328d6ef713b014.png) 映射其他目录和文件 ------------ >添加 file 文件夹下的文件 ```csharp app.UseStaticFiles(new StaticFileOptions { FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(),"file")) }); ``` >运行测试 ![](https://img.tnblog.net/arcimg/hb/ad3230d4f9944bbcaf2f2986c4e9bf8f.png) >添加自定义路径访问 ```csharp app.UseStaticFiles(new StaticFileOptions { RequestPath = "/files", FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(),"file")) }); ``` >运行测试 ![](https://img.tnblog.net/arcimg/hb/91c9cdeb4de64e95b172413fd08305ae.png) >添加到访问目录中去 ```csharp app.UseDirectoryBrowser(new DirectoryBrowserOptions { //推荐使用 new PathString("/files") 的方式 RequestPath = "/files", FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), "file")) }); ``` >运行测试 ![](https://img.tnblog.net/arcimg/hb/454bd975e542447f824e8b3dfe464a44.png) UseFileServer ------------ tn> UseFileServer 集合了 UseStaticFiles、UseDefaultFiles 以及 UseDirectoryBrowser >下面代码启用了静态目录和默认文件,但不允许直接访问目录: ```csharp app.UseFileServer(); ``` >下面代码启用了静态目录和默认文件,目录浏览的功能: ```csharp app.UseFileServer(enableDirectoryBrowsing:true); ``` >如果要指定具体的目录下 ```csharp app.UseFileServer(new FileServerOptions() { FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(),@"flie")), RequestPath=new PathString("/files"), EnableDirectoryBrowsing = true }); ``` tn>在启动浏览目录之前,需要添加`services.AddDirectoryBrowser();`目录的注册 FileExtensionContentTypeProvider ------------ tn>FileExtensionContentTypeProvider 类内包含一个将文件扩展名映射到 MIME 内容类型的集合。在下面的例子中,多个文件扩展名注册为已知的 MIME 类型,".rtf" 被替换,".mp4" 被移除 ```csharp var provider = new FileExtensionContentTypeProvider(); provider.Mappings[".myapp"] = "application/x-msdownload"; provider.Mappings[".htm3"] = "text/html"; provider.Mappings[".image"] = "image/png"; provider.Mappings[".rtf"] = "application/x-msdownload"; provider.Mappings.Remove(".mp4"); app.UseStaticFiles(new StaticFileOptions { RequestPath = "/files", FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), "file")), ContentTypeProvider = provider }); ``` >当我们有其他不可识别 MIME 的时候我们可以默认当作其他类型进行处理。 例如:有不可识别 MIME 的时候,默认让它用 image/png 来识别。 ```csharp app.UseStaticFiles(new StaticFileOptions { ServeUnknownFileTypes =true, DefaultContentType = "image/png" }); ``` 打造接口服务页 ------------ tn>当我们访问接口只希望指定格式为接口,其他访问都为首页时我们可以这样做 ```csharp app.MapWhen(context => { return !context.Request.Path.Value.StartsWith("/api"); }, appBuilder => { var option = new RewriteOptions(); option.AddRewrite(".*","/index.html",true); appBuilder.UseRewriter(option); appBuilder.UseStaticFiles(); }); ``` ![](https://img.tnblog.net/arcimg/hb/3f89f6b29ca64f39a1c4e823bec1fb36.png) ![](https://img.tnblog.net/arcimg/hb/9294d1c3f885441bb0024a47fad97c40.png)