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

Serving default documents

The frontend of modern web applications consists mostly of static files. Therefore, pointing the server to the client directory and instructing it to serve its entire content as static files is a common practice. In such situations, you will also want to send the user the main application file, such as index.html, when they navigate to the static folder. These files are called default documents.

Defining default documents for a static folder is done via the UseDefaultFiles method. Within the Startup.cs file, locate the Configure method and add the following marked line:

app.UseDefaultFiles();
app.UseStaticFiles();
UseDefaultFiles must be used before the UseStaticFiles call. If they are called in the opposite order, UseDefaultFiles will not have any affect.

The UseDefaultFiles method instructs ASP.NET Core to look for the following files once a URL that directs to a static folder has been detected:

  • default.htm
  • default.html
  • index.htm
  • index.html

These files will be looked for in that order. Once a filename matching one of these names is found, it will be served to the end user.

To change the filenames that UseDefaultFiles searches for, create and set a DefaultFilesOptions object and pass it to the method. For example, the following code changes the default filename collection to look for the main.htm and main.html files, instead of the predefined filenames:

DefaultFilesOptions options = new DefaultFilesOptions();
options.DefaultFileNames.Clear();
options.DefaultFileNames.Add("main.htm");
options.DefaultFileNames.Add("main.html");
app.UseDefaultFiles(options);

主站蜘蛛池模板: 苏尼特左旗| 林甸县| 巫山县| 黑河市| 南皮县| 梁平县| 高平市| 保定市| 安岳县| 天台县| 清涧县| 绍兴市| 峨边| 北流市| 阿鲁科尔沁旗| 洪雅县| 龙州县| 汉中市| 九龙城区| 柘城县| 苍山县| 湘乡市| 中方县| 北海市| 老河口市| 永德县| 平远县| 江油市| 湟中县| 伽师县| 临夏县| 辛集市| 磴口县| 霍林郭勒市| 满洲里市| 二手房| 中卫市| 兴宁市| 江山市| 攀枝花市| 绩溪县|