測試頁面foo.html 在Startup.cs內使用middleware 代碼如下: 或者: 運行結果: Static Files in ASP.NET Core By default an ASP.NET Core application will not serve static files; ...
測試頁面foo.html
在Startup.cs內使用middleware
代碼如下:
1 DefaultFilesOptions defaultFilesOptions = new DefaultFilesOptions(); 2 defaultFilesOptions.DefaultFileNames.Clear(); 3 defaultFilesOptions.DefaultFileNames.Add("foo.html"); 4 app.UseDefaultFiles(defaultFilesOptions); 5 app.UseStaticFiles();
或者:
1 FileServerOptions fileServerOptions = new FileServerOptions(); 2 fileServerOptions.DefaultFilesOptions.DefaultFileNames.Clear(); 3 fileServerOptions.DefaultFilesOptions.DefaultFileNames.Add("foo.html"); 4 app.UseFileServer(fileServerOptions);
運行結果:
Static Files in ASP.NET Core
By default an ASP.NET Core application will not serve static files; // 預設情況下ASP.NET Core應用程式時不支持靜態文件的。
The default directory for static files is wwwroot; //為靜態文件提供存儲的預設路徑時wwwroot;
To serve static files UseStaticFiles() middleware is required; //UseStaticFiles() 。在使用靜態文件時必須
To serve a default file UseDefaultFiles() middleware is required; //UserDefaultFiles() 。 在使用預設文件時必須
The following are the default files: //列舉了4種預設文件名
index.htm
index.html
default.htm
default.html
UseDefaultFiles() must be registered before UseStaticFiles(); //UseDefaultFiles()必須先於UseStatciFiles()註冊。
UseFileServer combines the functionality of UseStaticFiles,UseDefaultFiles and UseDirectoryBrowser middleware. //UseFileServer 集合了UseStaticFiles,UseDefaultFiles and UseDirectoryBrowser這些中間件的所有功能。
轉自油管>Static files in asp net core