默认情况下,提供静态文件index.html

ksp*_*rin 6 asp.net asp.net-core

我有一个非常简单的角度应用程序项目,只需要提供静态文件wwwroot.这是我的Startup.cs:

public class Startup
{
    public void ConfigureServices(IServiceCollection services) { }

    public void Configure(IApplicationBuilder app)
    {
        app.UseIISPlatformHandler();
        app.UseStaticFiles();
    }

    // Entry point for the application.
    public static void Main(string[] args) => WebApplication.Run<Startup>(args);
}
Run Code Online (Sandbox Code Playgroud)

每当我使用IIS Express或Web启动项目时,我总是必须导航到/index.html.我怎么做到这一点,我可以访问root(/)仍然得到index.html

Muh*_*eed 7

您想要服务器默认文件和静态文件:

public void Configure(IApplicationBuilder application)
{
    ...
    // Enable serving of static files from the wwwroot folder.
    application.UseStaticFiles();
    // Serve the default file, if present.
    application.UseDefaultFiles();
    ...
}
Run Code Online (Sandbox Code Playgroud)

或者,您可以使用UseFileServer使用单行而不是两行执行相同操作的方法.

public void Configure(IApplicationBuilder application)
{
    ...
    application.UseFileServer();
    ...
}
Run Code Online (Sandbox Code Playgroud)

有关更多信息,请参阅文档.

  • 您必须按此顺序执行此操作(“app.UseDefaultFiles”在“app.UseStaticFiles”之前)。否则使用“app.UseFileServer”,它只是同一件事的简写。 (2认同)

ksp*_*rin 6

只需app.UseStaticFiles();改为app.UseFileServer();

public class Startup
{
    public void ConfigureServices(IServiceCollection services) { }

    public void Configure(IApplicationBuilder app)
    {
        app.UseIISPlatformHandler();
        app.UseFileServer();
    }

    // Entry point for the application.
    public static void Main(string[] args) => WebApplication.Run<Startup>(args);
}
Run Code Online (Sandbox Code Playgroud)