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

6

我有一个非常简单的Angular应用项目,只需要从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);
}

每次我使用IIS Express或Web启动项目时,我都必须导航到/index.html。如何使我可以只访问根目录(/)而获得index.html
2个回答

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();
    ...
}

或者,您可以使用UseFileServer方法,它只需一行代码即可实现相同的功能,而不是两行。

public void Configure(IApplicationBuilder application)
{
    ...
    application.UseFileServer();
    ...
}

了解更多信息,请参阅文档


2
你必须按照这个顺序进行 (app.UseDefaultFilesapp.UseStaticFiles 之前)。否则使用 app.UseFileServer,它只是相同操作的简写。 - Chris Halcrow
@ChrisHalcrow 谢谢,现在已经修复了。作为以后的参考,你实际上可以直接编辑其他人的答案。 - Muhammad Rehan Saeed

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接