官术网_书友最值得收藏!

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")
});
If possible, try to avoid using static folders other than wwwroot. The wwwroot is the standard static directory in ASP.NET Core, and other developers who dive into your code will have an easier time understanding it if you stick to common practices.
主站蜘蛛池模板: 徐州市| 三江| 多伦县| 呼玛县| 昌乐县| 民县| 上蔡县| 九江市| 宁夏| 杭锦旗| 黄石市| 富蕴县| 石狮市| 北流市| 外汇| 奉贤区| 广东省| 元江| 准格尔旗| 蒙阴县| 进贤县| 万安县| 镇江市| 长阳| 辛集市| 山东| 安福县| 福海县| 宜都市| 东乡| 车险| 桑日县| 泰宁县| 伊通| 衢州市| 波密县| 偃师市| 论坛| 临泽县| 静海县| 乡城县|