asp .net core靜態(tài)文件資源的深入講解
前言
對(duì)靜態(tài)資源的簡(jiǎn)單的一個(gè)概況,在《重新整理.net core 計(jì)1400篇》系列后面會(huì)深入。
正文
我們?cè)诩尤胫虚g件是這樣寫的:
app.UseStaticFiles();
默認(rèn)是給wwwroot提供資源。
那么我訪問https://localhost:44330/js/site.js 資源,就可以訪問到。
// Please see documentation at https://docs.microsoft.com/aspnet/core/client-side/bundling-and-minification // for details on configuring this project to bundle and minify static web assets. // Write your JavaScript code.
同樣我們可以自定義路徑。
app.UseStaticFiles(new StaticFileOptions { FileProvider = new PhysicalFileProvider(Path.Combine(env.ContentRootPath, "Static")), RequestPath="/static" });
上面在根目錄下的static建立路由,路由路徑static 為標(biāo)識(shí)。
訪問:
https://localhost:44330/static/images/index.jpg
就能看到一張圖片了。
同樣再次訪問,https://localhost:44330/js/site.js 依然可以訪問,看了這個(gè)wwwroot 是一個(gè)釘子戶,無論如何添加還是存在的。
const string cacheMaxAge = "60480"; app.UseHttpsRedirection(); app.UseStaticFiles(new StaticFileOptions { FileProvider = new PhysicalFileProvider(Path.Combine(env.ContentRootPath, "Static")), RequestPath="/static", OnPrepareResponse = ctx => { ctx.Context.Response.Headers.Append("cache-control", $"public,max-age={cacheMaxAge}"); } } );
可以設(shè)置一些緩存。
靜態(tài)文件授權(quán)
官方倒是提供了兩種方法。
一種是,讓靜態(tài)文件路由放到權(quán)限之后。
app.UseAuthentication(); app.UseAuthorization(); app.UseStaticFiles(new StaticFileOptions { FileProvider = new PhysicalFileProvider( Path.Combine(env.ContentRootPath, "Static")), RequestPath = "/static" });
另一種比較自定義高:
[Authorize] public IActionResult BannerImage() { var filePath = Path.Combine( _env.ContentRootPath, "MyStaticFiles", "images", "red-rose.jpg"); return PhysicalFile(filePath, "image/jpeg"); }
可以根據(jù)參數(shù)做一些邏輯變化。
但是這些方式比較影響性能,一般來說靜態(tài)文件是開放的,而用戶上傳的文件是通過加密的,放在存儲(chǔ)服務(wù)器上。
當(dāng)然小型項(xiàng)目,可以用用。
靜態(tài)文件目錄
Configure中添加:
app.UseDirectoryBrowser(new DirectoryBrowserOptions { FileProvider=new PhysicalFileProvider(Path.Combine(env.ContentRootPath,"Static")), RequestPath="/static" });
這個(gè)中間件注入的位置是應(yīng)該在UseRouting之前的,同樣是性能問題。
然后在ConfigureServices中添加:
services.AddDirectoryBrowser();
效果:
這種方式呢,一般只是在dev環(huán)境下打開,真正的生產(chǎn)環(huán)境由于安全問題就不打開的。
默認(rèn)文檔
app.UseDefaultFiles(); app.UseStaticFiles();
app.UseStaticFiles(); 才是真正的路由。
app.UseDefaultFiles(); 只是說提供一些參數(shù),比如配置下面這些為默認(rèn)項(xiàng)。
default.htm default.html index.htm index.html
其實(shí)是這樣一個(gè)過程,app.UseStaticFiles() 如果沒有找到相應(yīng)的路由,那么應(yīng)該給下一個(gè)中間件。
如果調(diào)用了app.UseDefaultFiles(),那么會(huì)去找是否存在默認(rèn)項(xiàng),默認(rèn)是去wwwroot 下尋找上述的默認(rèn)項(xiàng)。
默認(rèn)文檔可以進(jìn)行修改:
var options = new DefaultFilesOptions(); options.DefaultFileNames.Clear(); options.DefaultFileNames.Add("mydefault.html"); app.UseDefaultFiles(options); app.UseStaticFiles();
UseFileServer 結(jié)合了 UseStaticFiles、UseDefaultFiles 和 UseDirectoryBrowser(可選)的功能。
app.UseFileServer(enableDirectoryBrowsing: true);
enableDirectoryBrowsing 表示是否使用UseDirectoryBrowser。
FileExtensionContentTypeProvider
FileExtensionContentTypeProvider 類包含 Mappings 屬性,用作文件擴(kuò)展名到 MIME 內(nèi)容類型的映射。
比如說我去訪問:https://localhost:44330/static/test.myapp
我在static 下有test.mapp 這個(gè)文件,但是靜態(tài)文件處理并沒有去處理。
原因:
客服端發(fā)了這樣一個(gè)請(qǐng)求,人家接受這些流,但是服務(wù)器找到到,myapp 對(duì)應(yīng)的媒體類型,那么這個(gè)時(shí)候客戶端就不會(huì)接受了,服務(wù)端也認(rèn)為沒有找到。
官方給了例子:
var provider = new FileExtensionContentTypeProvider(); // Add new mappings provider.Mappings[".myapp"] = "application/x-msdownload"; provider.Mappings[".htm3"] = "text/html"; provider.Mappings[".image"] = "image/png"; // Replace an existing mapping provider.Mappings[".rtf"] = "application/x-msdownload"; // Remove MP4 videos. provider.Mappings.Remove(".mp4"); app.UseDefaultFiles(); app.UseStaticFiles(); app.UseStaticFiles(new StaticFileOptions { FileProvider = new PhysicalFileProvider(Path.Combine(env.ContentRootPath, "Static")), RequestPath = "/static", OnPrepareResponse = ctx => { ctx.Context.Response.Headers.Append("cache-control", $"public,max-age={cacheMaxAge}"); }, ContentTypeProvider= provider }
給他加一個(gè)媒體類型,認(rèn)為myapp 應(yīng)該是一個(gè)需要下載文件。
然后運(yùn)行之,然后就會(huì)出現(xiàn)下載。
同樣,我們寫的是.html,如果我們不喜歡可以去寫.htm3也行。
https://localhost:44330/static/index.htm3
結(jié)果:
因?yàn)?code>provider.Mappings[".htm3"] = "text/html"; ,.htm3被映射成了text/html,那么客戶端就按照這種格式處理。所以模板引擎就可以多樣性,有興趣自己也可以去設(shè)計(jì)。
這就是媒體類型映射。
如果是媒體類型未知的情況下,那么可以這樣:
app.UseStaticFiles(new StaticFileOptions { FileProvider = new PhysicalFileProvider(Path.Combine(env.ContentRootPath, "Static")), RequestPath = "/static", OnPrepareResponse = ctx => { ctx.Context.Response.Headers.Append("cache-control", $"public,max-age={cacheMaxAge}"); }, ServeUnknownFileTypes = true, DefaultContentType = "image/png" } );
ServeUnknownFileTypes true
DefaultContentType "image/png" 讓客戶端按照?qǐng)D片處理。
但是官方給了建議。
啟用 ServeUnknownFileTypes 會(huì)形成安全隱患。 它默認(rèn)處于禁用狀態(tài),不建議使用。
FileExtensionContentTypeProvider 提供了更安全的替代方法來提供含非標(biāo)準(zhǔn)擴(kuò)展名的文件。
總結(jié)
到此這篇關(guān)于asp .net core靜態(tài)文件資源的文章就介紹到這了,更多相關(guān)asp .net core靜態(tài)文件資源內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
.NET調(diào)用控制臺(tái)下生成的exe文件,傳參及獲取返回參數(shù)的思路及代碼
.NET調(diào)用控制臺(tái)下生成的exe文件,傳參及獲取返回參數(shù)的思路及代碼,需要的朋友可以參考一下2013-06-06gridview和checkboxlist的嵌套相關(guān)應(yīng)用
gridview和checkboxlist的嵌套使用,會(huì)有效的提高開發(fā)的效率,不過很多的童鞋們對(duì)此還是很陌生的,接下來將幫助童鞋們實(shí)現(xiàn)gridview和checkboxlist的嵌套使用,感興趣的朋友可以了解下,或許對(duì)你有所幫助2013-02-02asp.net反射簡(jiǎn)單應(yīng)用實(shí)例
這篇文章主要介紹了asp.net反射簡(jiǎn)單應(yīng)用,結(jié)合完整實(shí)例形式分析了asp.net反射的原理與簡(jiǎn)單使用方法,需要的朋友可以參考下2017-02-02.NET實(shí)現(xiàn)WebSocket服務(wù)端即時(shí)通信實(shí)例
本篇文章主要介紹了.NET實(shí)現(xiàn)即時(shí)通信,WebSocket服務(wù)端實(shí)例 ,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-02-02asp.net GridView和DataList實(shí)現(xiàn)鼠標(biāo)移到行行變色
在設(shè)計(jì)頁面添加了DataList控件后,我在使用DataList綁定數(shù)據(jù)時(shí)是通過單元格來綁定的,因此鼠標(biāo)效果就在源代碼頁面去實(shí)現(xiàn)2009-02-02.NET開發(fā)人員關(guān)于ML.NET的入門學(xué)習(xí)
隨著谷歌,F(xiàn)acebook發(fā)布他們的工具機(jī)器學(xué)習(xí)工具Tensorflow 2和PyTorch,微軟也發(fā)布了ML.NET 1.0??梢哉f2019年是機(jī)器學(xué)習(xí)社區(qū)普及化的一年,下面小編向大家簡(jiǎn)單介紹一下關(guān)于ML.NET的入門學(xué)習(xí)2019-05-05ASP.NET中Application和Cache的區(qū)別分析
在asp.net中儲(chǔ)存數(shù)據(jù)的方式有很多,包括application,session,cache, cookie, viewstate。其中application和cache的應(yīng)用范圍,使用方式都比較相似,這里主要對(duì)比一下這兩種方式。2010-03-03ASP.NET中Webservice安全 實(shí)現(xiàn)訪問權(quán)限控制
本文主要講解ASP.NET中的Webservice的安全設(shè)置兩種方法,一種基于soapheader,一種基于SoapExtensionAttribute,需要的朋友可以參考下。2016-05-05SqlDataSource 鏈接Access 數(shù)據(jù)
如何用AccessDataSource鏈接有密碼的access數(shù)據(jù)庫總是會(huì)報(bào)錯(cuò),替代方法是用SqlDataSource 鏈接Access 數(shù)據(jù)。2009-04-04