靜態文件,包括HTML文件,CSS文件,圖像文件和JavaScript文件,它是一個應用裏所包含的資源。css
1. 提供靜態文件html
默認的,靜態文件存儲在你的webroot目錄下面,webroot的路徑定義在project.json裏面web
"webroot": "wwwroot"json
靜態文件能夠被存儲在webroot下面的任何目錄當中,以一個相對目錄獲取到。在一個默認的web應用程序當中,在webroot下面默認有三個目錄css, images和js. 要獲取圖像路徑,格式以下:瀏覽器
http://<yourApp>/images/<imageFileName>app
爲了應用靜態文件,你必須在Configure方法中註冊ide
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)ui
{spa
app.UseStaticFiles();htm
}
若是你有不在webroot目錄下的靜態文件須要添加,如何作呢
假如文件目錄以下:
* wwwroot
*css
*images
*....
*MyStaticFiles
* test.png
要獲取test.png,如何作呢,咱們須要在Configure添加以下的代碼:
app.UseStaticFiles(new StaticFileOptions()
{
FileProvider = new PhysicalFileProvider(@"D:\Source\WebApplication1\src\WebApplciation1\MyStaticFiles"),
RequestPath = new PathString("/StaticFiles")
});
這樣,用戶就能夠經過http://<yourApp>/StaticFiles/test.png來獲取test.png文件了.
2. 使目錄可預覽
上當瀏覽可使用戶瀏覽你的目錄和子目錄信息,默認的這個功能是關閉的,要使其打開,能夠在COnfigure裏增長以下代碼:
app.UseDirectoryBrowser();
一樣的,webroot目錄之外的靜態資源要被瀏覽,作以下配置:
app.UseDirectoryBrowser(new DirectoryBrowserOptions()
{
FileProvider = new PhysicalFileProvider(@"..."),
RequestPath = new PathString("/StaticFiles")
});
你能夠經過http://<yourApp>/StaticFiles去瀏覽.
3. 提供默認的文件
爲了避免輸入全路徑而要顯示默認的文件,能夠經過UseDefaultFiles擴展方法來擴展。注意,你還必須調用UseStaticFIles,這是由於UseDefaultFile是URL重寫,他實際上不提供文件。
app.UseDefaultFiles();
app.UseStaticFiles();
若是你用了UseDefaultFiles, 中間件會找以下的頁面做爲默認頁面。
*default.htm
*default.html
*index.htm
*index.html
要定義本身的默認文件,代碼以下:
DefaultFileOptions options = new DefaultFileOptions();
options.DefaultFileNames.Clear();
options.DefaultFileNames.Add("mydefault.html");
app.UseDefaultFiles(options);
app.UseStaticFiles();
若是你想用一個默認的頁面在webroot以外,你也必須再調用一下UseStaticFiles, UseDefaultFiles方法傳參數進去。這樣比較麻煩,能夠用UseFileServer代替
4. UseFileServer方法
UseFileServer方法能夠合併UseStaticFiles, UseDefaultFiles和UseDirectoryBrowser方法。
app.UseFileServer(); 或者 app.UseFileServer(enableDirectoryBrowsing: true);
你要在webroot以處定義你的靜態文件,代碼以下:
app.UserFileServer(new FileServerOptions()
{
FileProvider = new PhysicalFileProvider(@"D:\Source\WebApplication1\src\WebApplication1\MyStaticFiles"),
RequestPath = new PathString("/StaticFiles"),
EnableDirectoryBrowsing = true
});
5. Content types
ASP.NET提供了差很少400多個文件類型,若是你試圖訪問一個未知的文件類型,ASP.NET中間件不會提供這個文件。
例若有一個文件路徑
* wwwroot
* images
*test.image
若是你訪問http://<yourApp>/images/test.image, 你會獲得一個404的錯誤,儘管這個文件實際上存在。你能夠當開這個限制,以下所示:
app.UseStaticFiles(new StaticFileOptions
{
ServerUnknownFileTypes = true,
DefaultContentType = "image/png"
});
這樣,若是用戶瀏覽一個未定義的文件格式,瀏覽器會以一個圖片的格式在渲染它。
若是你有不少的文件類型是ASP.NET不知道,該怎麼作呢?FileExtensionContentTypeProvider類能夠幫助你。代碼以下:
var provider = new FileExtensionContentTypeProvider();
provider.Mappings.Add(".myapp", "application/x-msdownload");
app.UseStaticFiles(new StaticFileOptions{ ContentTypeProvider = provider});
這樣,若是用戶請求.myapp的文件,瀏覽器會下載該文件.
6. IIS
IIS有個本地的靜態文件的模塊,它獨立於ASP.NET的靜態文件,ASP.NET模塊在IIS本地模塊以前運行,在ASP.NET Beta7 , IIS 宿主改變了,若是ASP.NET不處理,IIS本地模塊也不處理。若是要選擇執行IIS本地的模塊,須要執行以下代碼:
app.RunIISPipeline();
注意:代碼文件應該放在webroot以外,這樣能夠把靜態文件和動態文件區分開來。