ASP.NET Core中的靜態(tài)文件介紹
靜態(tài)文件(HTML,CSS,圖片和Javascript之類的資源)會被ASP.NET Core應用直接提供給客戶端。
靜態(tài)文件通常位于網(wǎng)站根目錄(web root) <content-root>/wwwroot文件夾下。通常會把項目的當前目錄設置為Content root,這樣項目的web root就可以在開發(fā)階段被明確。
public static IWebHostBuilder CreateWebHostBuilder(string[] args) => WebHost.CreateDefaultBuilder(args) .UseContentRoot(Directory.GetCurrentDirectory()) //設置當前目錄 .UseStartup<Startup>();
靜態(tài)文件能夠被保存在網(wǎng)站根目錄下的任意文件夾內,并通過相對根的路徑來訪問。使用vs創(chuàng)建一個默認的Web應用程序時,在wwwroot目錄下會生成幾個文件夾:css,images,js。如果壓迫訪問images目錄下的圖片:
http://<app>/iamges/filename
https://localhost:44303/iamges/filename
要想使用靜態(tài)文件服務,必須配置中間件,把靜態(tài)文件中間件加入到管道。靜態(tài)文件一般會默認配置,在Configure方法中調用app.UseStaticFiles()。
app.UseStaticFiles() 使得web root(默認為wwwroot)下的文件可以被訪問。同時可以通過UseStaticFiles方法將其他目錄下的內容也可以向外提供:
假如wwwroot外面有一個MyStaticFiles文件夾,要訪問文件夾里面的資源test.png:
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { app.UseHttpsRedirection(); app.UseStaticFiles(); app.UseStaticFiles(new StaticFileOptions() { FileProvider = new PhysicalFileProvider( Path.Combine(Directory.GetCurrentDirectory(), @"MyStaticFiles")), //用于定位資源的文件系統(tǒng) RequestPath = new PathString("/StaticFiles") //請求地址 }); }
可以通過訪問
http://<app>/StaticFiles/test.png
https://localhost:44303/StaticFiles/test.png
1.靜態(tài)文件授權
靜態(tài)文件組件默認不提供授權檢查。任何通過靜態(tài)文件中間件訪問的文件都是公開的。要想給文件授權,可以將文件保存在wwwroot之外,并將目錄設置為可被靜態(tài)文件中間件能夠訪問,同時通過一個controller action來訪問文件,在action中授權后返回FileResult
。
2.目錄瀏覽
目錄瀏覽允許網(wǎng)站用戶看到指定目錄下的目錄和文件列表?;诎踩紤],默認情況下是禁止目錄訪問功能。在Startup.Configure中調用UseDirectoryBrowser擴展方法可以開啟網(wǎng)絡應用目錄瀏覽:
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { app.UseStaticFiles(); app.UseDirectoryBrowser(new DirectoryBrowserOptions() { FileProvider = new PhysicalFileProvider( Path.Combine(Directory.GetCurrentDirectory(),@"wwwroot\images")), RequestPath = new PathString("/MyImages") //如果不指定RequestPath,會將PhysicalFileProvider中的路徑參數(shù)作為默認文件夾,替換掉wwwroot }); }
然后在Startup.CongigureServices中調用AddDirectoryBrowser擴展方法。
這樣就可以通過訪問http://<app>/MyImages瀏覽wwwroot/images文件夾中的目錄,但是不能訪問文件:
要想訪問具體文件需要調用UseStaticFiles配置:
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { app.UseStaticFiles(); app.UseStaticFiles(new StaticFileOptions() { FileProvider = new PhysicalFileProvider( Path.Combine(Directory.GetCurrentDirectory(), @"wwwroot\images")), //用于定位資源的文件系統(tǒng) RequestPath = new PathString("/MyImages") }); app.UseDirectoryBrowser(new DirectoryBrowserOptions() { FileProvider = new PhysicalFileProvider( Path.Combine(Directory.GetCurrentDirectory(),@"wwwroot\images")), RequestPath = new PathString("/MyImages") }); }
3.默認文件
設置默認首頁能給站點的訪問者提供一個起始頁,在Startup.Configure中調用UseDefaFiles擴展方法:
app.UseDefaultFiles(options); app.UseStaticFiles();
UseDefaultFiles必須在UseStaticFiles之前調用。UseDefaultFiles只是重寫了URL,而不是真的提供了一個這樣的文件,瀏覽器URL將繼續(xù)顯示用戶輸入的URL。所以必須開啟靜態(tài)文件中間件。而且默認文件必須放在靜態(tài)文件中間件可以訪問得到的地方,默認是wwwroot中。
通過UseDefaultFiles,請求文件夾的時候檢索以下文件:
default.htm
default.html
index.htm
index.html
也可以使用UseDefaultFiles將默認頁面改為其他頁面:
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler("/Home/Error"); app.UseHsts(); } app.UseHttpsRedirection(); DefaultFilesOptions options = new DefaultFilesOptions(); options.DefaultFileNames.Clear(); options.DefaultFileNames.Add("mydefault.html"); app.UseDefaultFiles(options); app.UseStaticFiles(); app.UseMvc(routes => { routes.MapRoute( name: "default", template: "{controller=Home}/{action=Index}/{id?}"); }); }
4.UseFileServer
UseFileServer集合了UseStaticFiles,UseDefaultFiles,UseDirectoryBrowser。
調用app.UseFileServer(); 請用了靜態(tài)文件和默認文件,但不允許直接訪問目錄。需要調用app.UseFileServer(enableDirectoryBrowsing:true); 才能啟用目錄瀏覽功能。
如果想要訪問wwwroot以外的文件,需要配置一個FileServerOptions對象
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { app.UseStaticFiles();//如果不調用,將不會啟動默認功能。 app.UseFileServer(new FileServerOptions() { FileProvider = new PhysicalFileProvider( Path.Combine(Directory.GetCurrentDirectory(), @"MyStaticFiles")), RequestPath = new PathString("/StaticFiles"), EnableDirectoryBrowsing = true }); }
注意,如果將enableDirectoryBrowsing設置為true,需要在ConfigureServices中調用services.AddDirectoryBrowser();
如果默認文件夾下有默認頁面,將顯示默認頁面,而不是目錄列表。
5.FileExtensionContentTypeProvider
FileExtensionContentTypeProvider類包含一個將文件擴展名映射到MIME內容類型的集合。
例如:
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { var provider = new FileExtensionContentTypeProvider(); provider.Mappings[".htm3"] = "text/html"; provider.Mappings["images"] = "iamge/png"; provider.Mappings.Remove(".mp4"); app.UseStaticFiles(new StaticFileOptions() { FileProvider = new PhysicalFileProvider( Path.Combine(Directory.GetCurrentDirectory(), @"MyStaticFiles")), RequestPath = new PathString("/StaticFiles"), ContentTypeProvider = provider }); }
更多MIME類型可以訪問:http://www.iana.org/assignments/media-types/media-types.xhtml
6.非標準的內容類型
如果用戶請求了一個未知的文件類型,靜態(tài)文件中間件將會返回HTTP 404響應。如果啟用目錄瀏覽,則該文件的鏈接將會被顯示,但RUI會返回一個HTTP404錯誤。
使用UseStaticFiles方法可以將未知類型作為指定類型處理:
app.UseStaticFiles(new StaticFileOptions() { ServeUnknownFileTypes = true, DefaultContentType = "application/x-msdownload" });
對于未識別的,默認為application/x-msdownload,瀏覽器將會下載這些文件。
到此這篇關于ASP.NET Core靜態(tài)文件的文章就介紹到這了。希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
利用AJAX與數(shù)據(jù)島實現(xiàn)無刷新綁定
利用AJAX與數(shù)據(jù)島實現(xiàn)無刷新綁定...2007-03-03Asp.net中處理一個站點不同Web應用共享Session的問題
Asp.net中處理一個站點不同Web應用共享Session的問題...2006-09-09總結Visual Studio下ASP.NET模板化控件中的數(shù)據(jù)綁定
.NET框架中提供了很多數(shù)據(jù)綁定的組件,這里我們就來總結Visual Studio下ASP.NET模板化控件中的數(shù)據(jù)綁定,需要的朋友可以參考下2016-06-06ASP.NET實現(xiàn)根據(jù)IP獲取省市地址的方法
這篇文章主要介紹了ASP.NET實現(xiàn)根據(jù)IP獲取省市地址的方法,主要基于QQwry.dat純真IP數(shù)據(jù)庫來實現(xiàn)這一功能,非常實用,需要的朋友可以參考下2014-10-10Asp.net response對象與request對象使用介紹
這篇文章主要介紹了Asp.net response對象與request對象使用,需要的朋友可以參考下2014-04-04運行asp.net時出現(xiàn) http錯誤404-文件或目錄未找到
問題描述: http錯誤404-文件或目錄未找到的解決方法2009-03-03.NET一行代碼實現(xiàn)GC調優(yōu),讓程序不再占用內存
這篇文章主要介紹了NET一行代碼實現(xiàn)GC調優(yōu),讓程序不再占用內存的相關資料,需要的朋友可以參考下2022-11-11