1. 程式人生 > 實用技巧 >ASP.NET Core使用靜態檔案、目錄遊覽與MIME型別管理

ASP.NET Core使用靜態檔案、目錄遊覽與MIME型別管理

ASP.NET Core使用靜態檔案、目錄遊覽與MIME型別管理

Startup管道配置中,可以看到,注入相關靜態資源的程式碼

 //自定義自己的檔案路徑,例如提供訪問根目錄下的檔案,http://localhost:5000/preview/README.md
            //將訪問服務端的根目錄中的README.md檔案
            //var provider = new FileExtensionContentTypeProvider();
            //provider.Mappings.Remove(".png");

            //app.UseStaticFiles(new StaticFileOptions()
            
//{ // //Path.Combine(_webHostEnvironment.ContentRootPath, SiteSetting.DataFileDirectory, // //dataFile.DataFileType.ToString(), dataFile.CreateTime.Value.ToString("yyyyMM"), $"{dataFile.Id}{dataFile.FileExtenison}"); // FileProvider = new PhysicalFileProvider(env.ContentRootPath),
//指定實際物理路徑 // RequestPath = new PathString("/preview"),//對外的訪問路徑 // //OnPrepareResponse = ctx => // //{ // // //設定http響應快取為600秒 // // //max - age:表示當訪問此網頁後的max - age秒內再次訪問不會去伺服器請求,其功能與Expires類似,只是Expires是根據某個特定日期值做比較。一但快取者自身的時間不準確.則結果可能就是錯誤的,而max - age,顯然無此問題.。Max - age的優先順序也是高於Expires的
// // ctx.Context.Response.Headers.Append("Cache-Control", "public,max-age=600"); // //}, // //ContentTypeProvider = provider //濾掉一些敏感型別,比如.png,.exe等 //}); //瀏覽目錄的檔案與資料夾 //app.UseDirectoryBrowser(new DirectoryBrowserOptions()//提供檔案目錄訪問形式 //{ // FileProvider = new PhysicalFileProvider(env.ContentRootPath), // RequestPath = new PathString("/preview") //}); //直接開啟檔案目錄訪問和檔案訪問 app.UseFileServer(new FileServerOptions() { EnableDirectoryBrowsing = true,//開啟目錄訪問 FileProvider = new PhysicalFileProvider(env.ContentRootPath), RequestPath = new PathString("/preview") });