角网核应用程序重定向到/index.html而不是/

时间:2019-06-01 16:18:29

标签: angular redirect asp.net-core .net-core

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

当用户输入网址domain.com/时,他们将被重定向到domain.com/index.html

然后,当角度应用程序在网址中出现时,返回到domain.com/

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

将用户重定向到有角度的应用程序的方式是Startup.cs类中的这段代码(注意,我使用的是/而不是/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();
    }
});

1 个答案:

答案 0 :(得分:0)

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

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

此中间件导致从//index.html的重定向,以显示灵活的API。

为解决此问题,我在app.UseSwagger()之前的app.UseFileServer() 之前添加了https://flutter.dev/docs/development/platform-integration/platform-channels,其中包括使用静态文件和使用默认文件。这将提供文件内容,而无需首先引导浏览器。

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