ASP.NET Core中的靜態(tài)文件介紹
靜態(tài)文件(HTML,CSS,圖片和Javascript之類的資源)會(huì)被ASP.NET Core應(yīng)用直接提供給客戶端。
靜態(tài)文件通常位于網(wǎng)站根目錄(web root) <content-root>/wwwroot文件夾下。通常會(huì)把項(xiàng)目的當(dāng)前目錄設(shè)置為Content root,這樣項(xiàng)目的web root就可以在開發(fā)階段被明確。
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseContentRoot(Directory.GetCurrentDirectory()) //設(shè)置當(dāng)前目錄
.UseStartup<Startup>();靜態(tài)文件能夠被保存在網(wǎng)站根目錄下的任意文件夾內(nèi),并通過相對根的路徑來訪問。使用vs創(chuàng)建一個(gè)默認(rèn)的Web應(yīng)用程序時(shí),在wwwroot目錄下會(huì)生成幾個(gè)文件夾:css,images,js。如果壓迫訪問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)下的文件可以被訪問。同時(shí)可以通過UseStaticFiles方法將其他目錄下的內(nèi)容也可以向外提供:
假如wwwroot外面有一個(gè)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)文件授權(quán)
靜態(tài)文件組件默認(rèn)不提供授權(quán)檢查。任何通過靜態(tài)文件中間件訪問的文件都是公開的。要想給文件授權(quán),可以將文件保存在wwwroot之外,并將目錄設(shè)置為可被靜態(tài)文件中間件能夠訪問,同時(shí)通過一個(gè)controller action來訪問文件,在action中授權(quán)后返回FileResult。
2.目錄瀏覽
目錄瀏覽允許網(wǎng)站用戶看到指定目錄下的目錄和文件列表?;诎踩紤],默認(rèn)情況下是禁止目錄訪問功能。在Startup.Configure中調(diào)用UseDirectoryBrowser擴(kuò)展方法可以開啟網(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ò)展方法。
這樣就可以通過訪問http://<app>/MyImages瀏覽wwwroot/images文件夾中的目錄,但是不能訪問文件:

要想訪問具體文件需要調(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)首頁能給站點(diǎn)的訪問者提供一個(gè)起始頁,在Startup.Configure中調(diào)用UseDefaFiles擴(kuò)展方法:
app.UseDefaultFiles(options);
app.UseStaticFiles();UseDefaultFiles必須在UseStaticFiles之前調(diào)用。UseDefaultFiles只是重寫了URL,而不是真的提供了一個(gè)這樣的文件,瀏覽器URL將繼續(xù)顯示用戶輸入的URL。所以必須開啟靜態(tài)文件中間件。而且默認(rèn)文件必須放在靜態(tài)文件中間件可以訪問得到的地方,默認(rèn)是wwwroot中。
通過UseDefaultFiles,請求文件夾的時(shí)候檢索以下文件:
default.htm
default.html
index.htm
index.html
也可以使用UseDefaultFiles將默認(rèn)頁面改為其他頁面:
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(); 請用了靜態(tài)文件和默認(rèn)文件,但不允許直接訪問目錄。需要調(diào)用app.UseFileServer(enableDirectoryBrowsing:true); 才能啟用目錄瀏覽功能。
如果想要訪問wwwroot以外的文件,需要配置一個(gè)FileServerOptions對象
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)頁面,將顯示默認(rèn)頁面,而不是目錄列表。
5.FileExtensionContentTypeProvider
FileExtensionContentTypeProvider類包含一個(gè)將文件擴(kuò)展名映射到MIME內(nè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類型可以訪問:http://www.iana.org/assignments/media-types/media-types.xhtml
6.非標(biāo)準(zhǔn)的內(nèi)容類型
如果用戶請求了一個(gè)未知的文件類型,靜態(tài)文件中間件將會(huì)返回HTTP 404響應(yīng)。如果啟用目錄瀏覽,則該文件的鏈接將會(huì)被顯示,但RUI會(huì)返回一個(gè)HTTP404錯(cuò)誤。
使用UseStaticFiles方法可以將未知類型作為指定類型處理:
app.UseStaticFiles(new StaticFileOptions() {
ServeUnknownFileTypes = true,
DefaultContentType = "application/x-msdownload"
});對于未識(shí)別的,默認(rèn)為application/x-msdownload,瀏覽器將會(huì)下載這些文件。
到此這篇關(guān)于ASP.NET Core靜態(tài)文件的文章就介紹到這了。希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- ASP.NET?Core中的靜態(tài)文件
- 淺談ASP.NET Core靜態(tài)文件處理源碼探究
- 1個(gè)文件如何輕松搞定Asp.net core 3.1動(dòng)態(tài)頁面轉(zhuǎn)靜態(tài)頁面
- 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)用來發(fā)布靜態(tài)文件
相關(guān)文章
asp.net 數(shù)據(jù)庫備份還原(sqlserver+access)
Asp.net 備份、還原Ms SQLServer及壓縮Access數(shù)據(jù)庫2008-11-11
MongoDB.Net工具庫MongoRepository使用方法詳解
這篇文章主要為大家詳細(xì)介紹了MongoDB.Net工具庫MongoRepository的使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01
動(dòng)態(tài)生成table并實(shí)現(xiàn)分頁效果心得分享
動(dòng)態(tài)生成table并實(shí)現(xiàn)分頁在開發(fā)過程中時(shí)一個(gè)很好的應(yīng)用,接下來本文也要實(shí)現(xiàn)一個(gè)類似效果,感興趣的朋友可以參考下哈2013-04-04
asp.net 操作XML 按指定格式寫入XML數(shù)據(jù) WriteXml
從SQL下載數(shù)據(jù)到本地為XML文件2009-07-07
ASP.NET?Core中間件實(shí)現(xiàn)限流的代碼
這篇文章主要介紹了ASP.NET?Core中間件實(shí)現(xiàn)限流的方法,本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-03-03
Entity Framework使用Code First模式管理存儲(chǔ)過程
本文詳細(xì)講解了Entity Framework使用Code First模式管理存儲(chǔ)過程的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-03-03

