首页 > 解决方案 > 角度 netcore 应用程序重定向到 /index.html 而不是 /

问题描述

我有一个 Angular 7 + Asp.Net Core 2.2 网站。

当用户输入 url:domain.com/他们被重定向到domain.com/index.html,

然后,当 Angular 应用程序启动时,url 会返回到domain.com/.

如何防止跳转到 index.html 并使其看起来像这样:domain.com/从头到尾?

我将用户重定向到角度应用程序的方式是 Startup.cs 类中的这段代码(注意我使用/and not /index.html,它仍然重定向到/.index.html):

app.Use(async (context, next) =>
{
    await next();
    if (context.Response.StatusCode == 404 &&
    !Path.HasExtension(context.Request.Path.Value) &&
    !context.Request.Path.Value.StartsWith("/api/") &&
    {
        context.Request.Path = "/";
        await next();
    }
});

标签: angularredirectasp.net-core.net-core

解决方案


这可能会或可能不会解决您的问题,但我也遇到了同样的问题。对我来说,这是由标准的 swagger generation 中间件引起的Startup.cs

app.UseSwaggerUI(c =>
{
    c.SwaggerEndpoint("/swagger/v1/swagger.json", $"{nameof(SharingService)} API V1");
    c.RoutePrefix = "";
});

此中间件导致从/to重定向/index.html以显示 swagger api。

为了解决这个问题,我在之前添加了包括使用静态文件和使用默认文件的内容。这会提供文件内容,而无需先引导浏览器。app.UseFileServer() app.UseSwagger()

我还更新了要设置的 swagger 配置,c.RoutePrefix = "api"以便 swagger 页面在/api(而不是 root)可用。


推荐阅读