ASP.NET Core 设置默认起始页(如default.html)

测试页面foo.html

在Startup.cs内使用middleware

代码如下:

1             DefaultFilesOptions defaultFilesOptions = new DefaultFilesOptions();
2             defaultFilesOptions.DefaultFileNames.Clear();
3             defaultFilesOptions.DefaultFileNames.Add("foo.html");
4             app.UseDefaultFiles(defaultFilesOptions);
5             app.UseStaticFiles(); 

 

或者:

1             FileServerOptions fileServerOptions = new FileServerOptions();
2             fileServerOptions.DefaultFilesOptions.DefaultFileNames.Clear();
3             fileServerOptions.DefaultFilesOptions.DefaultFileNames.Add("foo.html");
4             app.UseFileServer(fileServerOptions);        

 

运行结果:

Static Files in ASP.NET Core

  By default an ASP.NET Core application will not serve static files; // 默认情况下ASP.NET Core应用程序时不支持静态文件的。

  The default directory for static files is wwwroot;         //为静态文件提供存储的默认路径时wwwroot;

  To serve static files UseStaticFiles() middleware is required;    //UseStaticFiles()  。在使用静态文件时必须

  To serve a default file UseDefaultFiles() middleware is required;    //UserDefaultFiles() 。 在使用默认文件时必须

  The following are the default files:               //列举了4种默认文件名

    index.htm

    index.html

    default.htm

    default.html

  UseDefaultFiles() must be registered before UseStaticFiles();  //UseDefaultFiles()必须先于UseStatciFiles()注册。

  UseFileServer combines the functionality of UseStaticFiles,UseDefaultFiles and UseDirectoryBrowser middleware. //UseFileServer 集合了UseStaticFiles,UseDefaultFiles and UseDirectoryBrowser这些中间件的所有功能。

  

  转自油管>Static files in asp net core

 

  

  

  

  

  

posted @ 2019-08-06 20:49  BrainK_1400  阅读(8170)  评论(0编辑  收藏  举报