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

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);

主站蜘蛛池模板: 犍为县| 土默特右旗| 望城县| 三门峡市| 迁安市| 吴桥县| 山西省| 清河县| 香港 | 闻喜县| 会昌县| 徐水县| 河池市| 庄河市| 合江县| 东港市| 盐边县| 依兰县| 马龙县| 怀柔区| 惠东县| 伊宁市| 崇文区| 荣成市| 万山特区| 莆田市| 汤阴县| 湖南省| 孝感市| 错那县| 达尔| 确山县| 宾阳县| 普定县| 江川县| 安仁县| 三门峡市| 永寿县| 宜兰县| 宜君县| 虹口区|