來源
這些HTML , CSS files, image files, 和JavaScript這些靜態文件,是ASP.NET能夠直接響應給客戶端的。本文詳述下ASP.NET和靜態文件的關系。
Serving static files##
靜態文件存儲在項目的webroot下,webroot的位置可以通過project.json文件來配置。
"webroot": "wwwroot"
靜態文件可以存儲在任何位置,可以通過相對路徑來獲取。webroot下的靜態文件可以通過http://
為了能夠提供靜態文件,必須配置中間件來將靜態文件添加到管道中。可以通過在Startup類中的Configre方法里調用UseStaticFiles擴展方法來實現。如下
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
...
// Add static files to the request pipeline.
app.UseStaticFiles();
...
如果你想提供webroot之外的靜態文件,項目結構如下:
- wwwroot
- css
- img
- js
- MyStaticFiles
- test.png
為了使用戶能夠訪問test.png文件,必須如下配置靜態文件:
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
...
// Add MyStaticFiles static files to the request pipeline.
app.UseStaticFiles(new StaticFileOptions()
{
FileProvider = new PhysicalFileProvider(@"D:\Source\WebApplication1\src\WebApplication1\MyStaticFiles"),
RequestPath = new PathString("/StaticFiles")
});
...
這時如果用戶訪問http://
文件目錄瀏覽##
默認情況下,該功能是不可用的。開啟該功能,需要在Startup.Configure中調用UseDirectoryBrowser方法,如下
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
...
// Turn on directory browsing for the current directory.
app.UseDirectoryBrowser();
...
瀏覽項目的images文件夾,結果如下:
如果有個文件夾在webroot之外,但是需要用戶可瀏覽。項目結構如下:
- wwwroot
- css
- img
- js
- MyStaticFiles
為了使用戶可瀏覽MyStaticFiles目錄,需要如下配置
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
...
// Add the ability for the user to browse the MyStaticFiles directory.
app.UseDirectoryBrowser(new DirectoryBrowserOptions()
{
FileProvider = new PhysicalFileProvider(@"D:\Source\WebApplication1\src\WebApplication1\MyStaticFiles"),
RequestPath = new PathString("/StaticFiles")
});
...
這時,如果如果用戶訪問地址http://
Serving default files##
在用戶沒有輸入完整的URI的情況下,為了提供給用戶默認的文件,需要在Startup.Configure中調用UseDefaultFiles。但同事也必須調用UseStaticFiles方法,這是因為UseDefaultFiles是url重寫,並不提供文件服務。
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
...
// Serve the default file, if present.
app.UseDefaultFiles();
app.UseStaticFiles();
...
如果你調用了UseDefaultFiles方法,如果用戶輸入了某個文件的路徑,中間件會按順序搜索如下文件。如果找到了,文件就會返回,就如同用戶輸入了完整路徑一樣。
- default.htm
- default.html
- index.htm
- index.html
為了指定其他默認文件,需要實例化DefaultFilesOptions對象,並設置其參數DefaultFileNames列表. 然后調用UseDefaultFiles,並把需要實例化DefaultFilesOptions對象對象傳給它。下例,移除了DefaultFileNames列表中的默認的文件,並把mydefault.html添加作為唯一的默認文件。
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
...
// Serve my app-specific default file, if present.
DefaultFilesOptions options = new DefaultFilesOptions();
options.DefaultFileNames.Clear();
options.DefaultFileNames.Add("mydefault.html");
app.UseDefaultFiles(options);
app.UseStaticFiles();
...
此時,如果你瀏覽webroot中的文件夾,並攜帶文件名mydefault.html,該文件就可以獲取,就如同用戶輸入了全路徑。
但是如果你想設置的默認文件在webroot之外,你需要調用UseStaticFiles和UseDefaultFiles,並傳給他們相對的參數值。然而更方便和推薦的方法是使用
UseFileServer方法。
使用UseFileServer方法##
除了UseStaticFiles, UseDefaultFiles和 UseDirectoryBrowser擴展方法,還有UseFileServer方法,該方法結合了上面三個方法的功能。
// Enable all static file middleware (serving of static files and default files) EXCEPT directory browsing.
app.UseFileServer();
// Enables all static file middleware (serving of static files, default files, and directory browsing).
app.UseFileServer(enableDirectoryBrowsing: true);
如同UseStaticFiles, UseDefaultFiles和UseDirectoryBrowser,如果想服務webroot之外的文件,需要實例化和配置一個“options”對象,並作為參數傳給UseFileServer。比如,有如下結構
- wwwroot
- css
- img
- js
- MyStaticFiles
- test.png
- default.html
如上結構,如果想提供靜態文件,默認文件以及目錄瀏覽功能,如下只需調用UseFileServer方法。
app.UseFileServer(new FileServerOptions()
{
FileProvider = new PhysicalFileProvider(@"D:\Source\WebApplication1\src\WebApplication1\MyStaticFiles"),
RequestPath = new PathString("/StaticFiles"),
EnableDirectoryBrowsing = true
});
如上例,如果用戶訪問如下路徑
- http://
/StaticFiles/test.png - MyStaticFiles/test.png 文件會提供給瀏覽器 - http://
/StaticFiles - 因為有默認文件default.html,該文件會提供給瀏覽器。如果不存在默認文件,瀏覽器會提供文件瀏覽(因為 FileServerOptions.EnableDirectoryBrowsing屬性設置為了true).
文件類型##
ASP.NET靜態文件中間件提供了接近400種已知的文件類型。如果用戶嘗試獲取位置類型的文件,ASP.NET不會嘗試提供服務。
假設如下目錄
- wwwroot
- css
- js
- images
- test.image
按如下配置
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
...
// Serve static files and allow directory browsing.
app.UseDirectoryBrowser();
app.UseStaticFiles();
如果訪問http://
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
...
// Serve static files and allow directory browsing.
app.UseDirectoryBrowser();
app.UseStaticFiles(new StaticFileOptions
{
ServeUnknownFileTypes = true,
DefaultContentType = "image/png"
});
這是如果瀏覽器訪問未知類型的文件,服務器會將他們作為image/png類型來對待。
目前為止,在處理為未知類型文件時,已經指定了默認文件類型。但是如果有多種未知文件類型呢。這就是FileExtensionContentTypeProvider類發揮作用的時候了。這個類內部有一個集合,來映射文件擴展名和MIME文件類型。為了自定義文件類型,只需實例化FileExtensionContentTypeProvider,並添加映射到FileExtensionContentTypeProvider.Mappings字典。如下例,添加了擴展.myapp到application/x-msdownload MIME類型。
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
...
// Allow directory browsing.
app.UseDirectoryBrowser();
// Set up custom content types - associating file extension to MIME type
var provider = new FileExtensionContentTypeProvider();
provider.Mappings.Add(".myapp", "application/x-msdownload");
// Serve static files.
app.UseStaticFiles(new StaticFileOptions { ContentTypeProvider = provider });
...
現在如果用戶嘗試瀏覽.myapp后綴名的文件,用戶會被提示下載該文件。
IIS Considerations##
IIS有自己的靜態文件處理模塊,它是獨立與上文提到的ASP.NET靜態文件中間件的。ASP.NET模塊是在IIS本地模塊之前運行的。在ASP.NET Beta 7中,IIS host改變了運行方式,如果請求沒有被ASP.NET所處理,將會返回404錯誤,而不是交給IIS自身模塊來處理。為了運行IIS自身模塊,需如下配置。
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
...
...
// Enable the IIS native module to run after the ASP.NET middleware components.
// This call should be placed at the end of your Startup.Configure method so that
// it doesn't interfere with other middleware functionality.
app.RunIISPipeline();
}
最佳實踐##
代碼文件(包括C#和Razor文件)應該放在項目的webroot之外,這樣能夠很好的隔離。
總結##
In this article, you learned how the static files middleware component in ASP.NET 5 allows you to serve static files, enable directory browsing, and serve default files. You also saw how to work with content types that ASP.NET doesn’t recognize. Finally, the article explained some IIS considerations and presented some best practices for working with static files.