一、概述
靜態文件(如 HTML、CSS、圖片和 JavaScript等文件)是 Web程序直接提供給客戶端的直接加載的文件。 較比於程序動態交互的代碼而言,其實原理都一樣(走Http協議),
ASP.NET Core中需要進行一些配置才能提供這些文件。
二、wwwroot
靜態文件存儲在項目的 Web 程序的 {ContentRoot}/wwwroot目錄下,但可通過 UseWebRoot 方法更改路徑 。
Web 應用程序項目的 wwwroot 文件夾中默認有多個文件夾 :
- wwwroot
- css
- images
- js
用於訪問 images 子文件夾中的文件的 URI 格式為 http://<server_address>/images/<image_file_name> 。 例如, http://localhost:1189/images/banner.png。
通過以下代碼開啟靜態文件訪問功能(設置 {ContentRoot}/wwwroot為默認的靜態文件工作目錄)
public void Configure(IApplicationBuilder app) { app.UseStaticFiles(); }
三、設置指定目錄為靜態文件工作目錄
public void Configure(IApplicationBuilder app) { app.UseStaticFiles(); // wwwroot 目錄 app.UseStaticFiles(new StaticFileOptions { FileProvider = new PhysicalFileProvider( Path.Combine(Directory.GetCurrentDirectory(), "the_path_to_yours")), RequestPath = "/GiveAName" }); }
注意訪問自定義的靜態文件路徑發生變化:http://localhost:1189/GiveAName/images/banner.png
四、給靜態文件添加客戶端緩存
public void Configure(IApplicationBuilder app, IHostingEnvironment env) { var cachePeriod = env.Production() ? "60000" : "600"; app.UseStaticFiles(new StaticFileOptions { OnPrepareResponse = ctx => { // 記得下面的引用: // using Microsoft.AspNetCore.Http; ctx.Context.Response.Headers.Append("Cache-Control", $"public, max-age={cachePeriod}"); } }); }
如上,在生產環境中,我們給靜態文件添加了60000(ms)的緩存,即:在一分鍾內,客戶端都會從瀏覽器本地拿文件,注意此手法,直接更新靜態文件一時間是拿不到最新文件的.
五、利用PhysicalFile方法讓靜態文件的訪問可以進行鑒權
靜態文件中間件允許瀏覽器端訪問由靜態文件中間件提供的所有靜態文件(包括 wwwroot 下的文件),我們設想實際的一個應用場景,我們上傳了一個文件到指定目錄,而這個文件只能當事人自己可以進行訪問,那么如何進行權限驗證呢?
[Authorize]//身份驗證 public IActionResult UsersOwnPictrue() { var file = Path.Combine(Directory.GetCurrentDirectory(), "StaticFilesPath", "images", "my.svg"); return PhysicalFile(file, "image/svg+xml");//返回靜態文件 }
六、前后端分離開發中的使用
前后端開發分離的開發模式中,前端自己負責前端的一切交互工作,不僅如此還會在后端工作沒有啟動前自己構造數據,ASP.NET Core靜態文件中間件,正好可和此開發
模式進行銜接,此處省略一萬字,有一點大家比較關心的問題:如何設置項目起始頁,如何設置前端項目中的默認頁
public void Configure(IApplicationBuilder app) { app.UseDefaultFiles();//必須在UseStaticFiles
前調用UseDefaultFiles
。UseDefaultFiles
實際上用於重寫 URL,不提供文件。 app.UseStaticFiles(); //這兩步 }
使用 UseDefaultFiles 會使用以下這些文件作為整個項目的默認起始頁: default.htm default.html index.htm index.html
public void Configure(IApplicationBuilder app) { // 使用其他文件作為默認靜態頁面文件 DefaultFilesOptions options = new DefaultFilesOptions(); options.DefaultFileNames.Clear(); options.DefaultFileNames.Add("其他wwwroot下的.html"); app.UseDefaultFiles(options); app.UseStaticFiles(); }
app.UseFileServer();//可將上面兩步何為一步
七、關於UseFileServer
UseFileServer 整合了 UseStaticFiles、UseDefaultFiles 和 UseDirectoryBrowser(可選,如果啟用需要services.AddDirectoryBrowser())的功能,
public void Configure(IApplicationBuilder app) { app.UseFileServer(new FileServerOptions { FileProvider = new PhysicalFileProvider( Path.Combine(Directory.GetCurrentDirectory(), "文件夾")), RequestPath = "/請求名", EnableDirectoryBrowsing = true }); }
請求連接 | 對應路徑 |
http://<server_address>/請求名/images/file1.png | 文件夾/images/file1.png |
http://<server_address>/請求名/ | 文件夾/default.html |
八、開啟目錄瀏覽功能
此功能通常不要開啟,因為比較危險,通過目錄瀏覽,Web 應用的用戶可查看目錄列表和指定目錄中的文件。 出於安全考慮,調用 Startup.Configure
中的 UseDirectoryBrowser 方法來啟用目錄瀏覽:
public void ConfigureServices(IServiceCollection services) { services.AddDirectoryBrowser(); }
app.UseDirectoryBrowser(new DirectoryBrowserOptions //開啟對wwwroot/images的文件進行瀏覽的功能 { FileProvider = new PhysicalFileProvider( Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "images")), RequestPath = "/MyImages" });
九、指定指定文件類型的MIME類型
public void Configure(IApplicationBuilder app) { var provider = new FileExtensionContentTypeProvider(); provider.Mappings[".rtf"] = "application/x-msdownload"; // 移除指定文件的解析 provider.Mappings.Remove(".mp4"); ...... }
public void Configure(IApplicationBuilder app) { app.UseStaticFiles(new StaticFileOptions { //未知類型的ContentType ServeUnknownFileTypes = true, DefaultContentType = "bala/your type" }); }