- Hands-On Full:Stack Web Development with ASP.NET Core
- Tamir Dresher Amir Zuker Shay Friedman
- 294字
- 2021-06-10 19:37:24
Setting the server to serve static files
ASP.NET Core, by default, doesn't serve static files to end users. Even though the content root and web root paths have default values, you have to explicitly indicate that static files should be served. To do so, open the Startup.cs file, locate the Configure method, and add the following line:
app.UseStaticFiles();
Once this is placed in the right location, the ASP.NET Core platform will serve static files from the web root folder.
Make sure that the web root folder itself is not part of the URL. The following table demonstrates the physical file location and the matching URL, assuming that the default web root path, wwwroot, hasn't been changed:

In addition to the Web root folder, it is possible to serve static files that reside in other directories. In order to do so, you need to pass parameters to the UseStaticFiles method. For example, the following code sample sets the assets folder as a static file folder under the /assets URL:
app.UseStaticFiles(new StaticFileOptions()
{
FileProvider = new PhysicalFileProvider(
Path.Combine(Directory.GetCurrentDirectory(), "assets")),
RequestPath = new PathString("/assets")
});
To add multiple static file paths, call the UseStaticFiles method multiple times. For example, the following piece of code sets the default Web root as a static file folder, as well as two other folders, images and videos:
app.UseStaticFiles(); // web root
app.UseStaticFiles(new StaticFileOptions()
{
FileProvider = new PhysicalFileProvider(
Path.Combine(Directory.GetCurrentDirectory(), "images")),
RequestPath = new PathString("/images")
});
app.UseStaticFiles(new StaticFileOptions()
{
FileProvider = new PhysicalFileProvider(
Path.Combine(Directory.GetCurrentDirectory(), "videos")),
RequestPath = new PathString("/videos")
});
- 6G潛在關鍵技術(下冊)
- SEO 20日
- 智慧光網絡:關鍵技術、應用實踐和未來演進
- Microsoft Dynamics CRM 2011 Applications(MB2-868) Certification Guide
- 工業(yè)互聯(lián)網創(chuàng)新實踐
- 園區(qū)網絡架構與技術
- 云計算技術與標準化
- 基于IPv6的家居物聯(lián)網開發(fā)與應用技術
- bash網絡安全運維
- Hands-On Microservices:Monitoring and Testing
- 互聯(lián)網視覺設計(全彩慕課版)
- 賽博空間簡史
- SNS網站構建
- 計算機網絡技術實用寶典(第3版)
- 計算機聯(lián)鎖及信號微機監(jiān)測系統(tǒng)