ASP.NET Core中的靜態(tài)文件介紹
靜態(tài)文件(HTML,CSS,圖片和Javascript之類(lèi)的資源)會(huì)被ASP.NET Core應(yīng)用直接提供給客戶(hù)端。
靜態(tài)文件通常位于網(wǎng)站根目錄(web root) <content-root>/wwwroot文件夾下。通常會(huì)把項(xiàng)目的當(dāng)前目錄設(shè)置為Content root,這樣項(xiàng)目的web root就可以在開(kāi)發(fā)階段被明確。
public static IWebHostBuilder CreateWebHostBuilder(string[] args) => WebHost.CreateDefaultBuilder(args) .UseContentRoot(Directory.GetCurrentDirectory()) //設(shè)置當(dāng)前目錄 .UseStartup<Startup>();
靜態(tài)文件能夠被保存在網(wǎng)站根目錄下的任意文件夾內(nèi),并通過(guò)相對(duì)根的路徑來(lái)訪(fǎng)問(wèn)。使用vs創(chuàng)建一個(gè)默認(rèn)的Web應(yīng)用程序時(shí),在wwwroot目錄下會(huì)生成幾個(gè)文件夾:css,images,js。如果壓迫訪(fǎng)問(wèn)images目錄下的圖片:
http://<app>/iamges/filename
https://localhost:44303/iamges/filename
要想使用靜態(tài)文件服務(wù),必須配置中間件,把靜態(tài)文件中間件加入到管道。靜態(tài)文件一般會(huì)默認(rèn)配置,在Configure方法中調(diào)用app.UseStaticFiles()。
app.UseStaticFiles() 使得web root(默認(rèn)為wwwroot)下的文件可以被訪(fǎng)問(wèn)。同時(shí)可以通過(guò)UseStaticFiles方法將其他目錄下的內(nèi)容也可以向外提供:
假如wwwroot外面有一個(gè)MyStaticFiles文件夾,要訪(fǎng)問(wèn)文件夾里面的資源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") //請(qǐng)求地址 }); }
可以通過(guò)訪(fǎng)問(wèn)
http://<app>/StaticFiles/test.png
https://localhost:44303/StaticFiles/test.png
1.靜態(tài)文件授權(quán)
靜態(tài)文件組件默認(rèn)不提供授權(quán)檢查。任何通過(guò)靜態(tài)文件中間件訪(fǎng)問(wèn)的文件都是公開(kāi)的。要想給文件授權(quán),可以將文件保存在wwwroot之外,并將目錄設(shè)置為可被靜態(tài)文件中間件能夠訪(fǎng)問(wèn),同時(shí)通過(guò)一個(gè)controller action來(lái)訪(fǎng)問(wèn)文件,在action中授權(quán)后返回FileResult
。
2.目錄瀏覽
目錄瀏覽允許網(wǎng)站用戶(hù)看到指定目錄下的目錄和文件列表?;诎踩紤],默認(rèn)情況下是禁止目錄訪(fǎng)問(wèn)功能。在Startup.Configure中調(diào)用UseDirectoryBrowser擴(kuò)展方法可以開(kāi)啟網(wǎng)絡(luò)應(yī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,會(huì)將PhysicalFileProvider中的路徑參數(shù)作為默認(rèn)文件夾,替換掉wwwroot }); }
然后在Startup.CongigureServices中調(diào)用AddDirectoryBrowser擴(kuò)展方法。
這樣就可以通過(guò)訪(fǎng)問(wèn)http://<app>/MyImages瀏覽wwwroot/images文件夾中的目錄,但是不能訪(fǎng)問(wèn)文件:
要想訪(fǎng)問(wèn)具體文件需要調(diào)用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.默認(rèn)文件
設(shè)置默認(rèn)首頁(yè)能給站點(diǎn)的訪(fǎng)問(wèn)者提供一個(gè)起始頁(yè),在Startup.Configure中調(diào)用UseDefaFiles擴(kuò)展方法:
app.UseDefaultFiles(options); app.UseStaticFiles();
UseDefaultFiles必須在UseStaticFiles之前調(diào)用。UseDefaultFiles只是重寫(xiě)了URL,而不是真的提供了一個(gè)這樣的文件,瀏覽器URL將繼續(xù)顯示用戶(hù)輸入的URL。所以必須開(kāi)啟靜態(tài)文件中間件。而且默認(rèn)文件必須放在靜態(tài)文件中間件可以訪(fǎng)問(wèn)得到的地方,默認(rèn)是wwwroot中。
通過(guò)UseDefaultFiles,請(qǐng)求文件夾的時(shí)候檢索以下文件:
default.htm
default.html
index.htm
index.html
也可以使用UseDefaultFiles將默認(rèn)頁(yè)面改為其他頁(yè)面:
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。
調(diào)用app.UseFileServer(); 請(qǐng)用了靜態(tài)文件和默認(rèn)文件,但不允許直接訪(fǎng)問(wèn)目錄。需要調(diào)用app.UseFileServer(enableDirectoryBrowsing:true); 才能啟用目錄瀏覽功能。
如果想要訪(fǎng)問(wèn)wwwroot以外的文件,需要配置一個(gè)FileServerOptions對(duì)象
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { app.UseStaticFiles();//如果不調(diào)用,將不會(huì)啟動(dòng)默認(rèn)功能。 app.UseFileServer(new FileServerOptions() { FileProvider = new PhysicalFileProvider( Path.Combine(Directory.GetCurrentDirectory(), @"MyStaticFiles")), RequestPath = new PathString("/StaticFiles"), EnableDirectoryBrowsing = true }); }
注意,如果將enableDirectoryBrowsing設(shè)置為true,需要在ConfigureServices中調(diào)用services.AddDirectoryBrowser();
如果默認(rèn)文件夾下有默認(rèn)頁(yè)面,將顯示默認(rèn)頁(yè)面,而不是目錄列表。
5.FileExtensionContentTypeProvider
FileExtensionContentTypeProvider類(lèi)包含一個(gè)將文件擴(kuò)展名映射到MIME內(nèi)容類(lèi)型的集合。
例如:
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類(lèi)型可以訪(fǎng)問(wèn):http://www.iana.org/assignments/media-types/media-types.xhtml
6.非標(biāo)準(zhǔn)的內(nèi)容類(lèi)型
如果用戶(hù)請(qǐng)求了一個(gè)未知的文件類(lèi)型,靜態(tài)文件中間件將會(huì)返回HTTP 404響應(yīng)。如果啟用目錄瀏覽,則該文件的鏈接將會(huì)被顯示,但RUI會(huì)返回一個(gè)HTTP404錯(cuò)誤。
使用UseStaticFiles方法可以將未知類(lèi)型作為指定類(lèi)型處理:
app.UseStaticFiles(new StaticFileOptions() { ServeUnknownFileTypes = true, DefaultContentType = "application/x-msdownload" });
對(duì)于未識(shí)別的,默認(rèn)為application/x-msdownload,瀏覽器將會(huì)下載這些文件。
到此這篇關(guān)于ASP.NET Core靜態(tài)文件的文章就介紹到這了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- ASP.NET?Core中的靜態(tài)文件
- 淺談ASP.NET Core靜態(tài)文件處理源碼探究
- 1個(gè)文件如何輕松搞定Asp.net core 3.1動(dòng)態(tài)頁(yè)面轉(zhuǎn)靜態(tài)頁(yè)面
- ASP.NET Core 應(yīng)用程序中的靜態(tài)文件中間件的實(shí)現(xiàn)
- ASP.NET Core靜態(tài)文件的使用方法
- ASP.NET Core中預(yù)壓縮靜態(tài)文件的方法步驟
- ASP.NET Core靜態(tài)文件使用教程(9)
- 解析如何利用一個(gè)ASP.NET Core應(yīng)用來(lái)發(fā)布靜態(tài)文件
相關(guān)文章
利用AJAX與數(shù)據(jù)島實(shí)現(xiàn)無(wú)刷新綁定
利用AJAX與數(shù)據(jù)島實(shí)現(xiàn)無(wú)刷新綁定...2007-03-03Asp.net中處理一個(gè)站點(diǎn)不同Web應(yīng)用共享Session的問(wèn)題
Asp.net中處理一個(gè)站點(diǎn)不同Web應(yīng)用共享Session的問(wèn)題...2006-09-09總結(jié)Visual Studio下ASP.NET模板化控件中的數(shù)據(jù)綁定
.NET框架中提供了很多數(shù)據(jù)綁定的組件,這里我們就來(lái)總結(jié)Visual Studio下ASP.NET模板化控件中的數(shù)據(jù)綁定,需要的朋友可以參考下2016-06-06教你使用.NET快速比較兩個(gè)byte數(shù)組是否相等
在.NET中如何快速的比較兩個(gè)byte數(shù)組是否完全相等,聽(tīng)起來(lái)是一個(gè)比較兩個(gè)byte數(shù)組是完全相等是一個(gè)簡(jiǎn)單的問(wèn)題,但是深入研究以后,覺(jué)得還是有很多方案的,這里和大家一起分享下2022-04-04ASP.NET實(shí)現(xiàn)根據(jù)IP獲取省市地址的方法
這篇文章主要介紹了ASP.NET實(shí)現(xiàn)根據(jù)IP獲取省市地址的方法,主要基于QQwry.dat純真IP數(shù)據(jù)庫(kù)來(lái)實(shí)現(xiàn)這一功能,非常實(shí)用,需要的朋友可以參考下2014-10-10Asp.net response對(duì)象與request對(duì)象使用介紹
這篇文章主要介紹了Asp.net response對(duì)象與request對(duì)象使用,需要的朋友可以參考下2014-04-04運(yùn)行asp.net時(shí)出現(xiàn) http錯(cuò)誤404-文件或目錄未找到
問(wèn)題描述: http錯(cuò)誤404-文件或目錄未找到的解決方法2009-03-03.NET一行代碼實(shí)現(xiàn)GC調(diào)優(yōu),讓程序不再占用內(nèi)存
這篇文章主要介紹了NET一行代碼實(shí)現(xiàn)GC調(diào)優(yōu),讓程序不再占用內(nèi)存的相關(guān)資料,需要的朋友可以參考下2022-11-11mvc上傳到美橙云虛擬機(jī)系列問(wèn)題的解決方法
這篇文章主要為大家詳細(xì)介紹了mvc上傳到美橙云虛擬機(jī)系列問(wèn)題的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10