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

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.
主站蜘蛛池模板: 南平市| 西丰县| 天津市| 灵川县| 宜黄县| 锡林郭勒盟| 永昌县| 荔波县| 桃江县| 河南省| 双城市| 左贡县| 枞阳县| 高青县| 怀来县| 修文县| 南开区| 彝良县| 阿拉善右旗| 紫云| 台前县| 新河县| 霸州市| 师宗县| 伊吾县| 鹤庆县| 巴中市| 平阴县| 涞源县| 文山县| 鸡泽县| 南宁市| 沐川县| 韩城市| 鸡东县| 四川省| 霍林郭勒市| 比如县| 曲阳县| 青浦区| 京山县|