Asp.net core 3.1 将空路由重定向到默认语言路由

Asp.net core 3.1 redirect empty route to default language route

我在我的项目中使用多语言 angular 应用程序如下:

app.Map("/en", en =>
{
    en.UseSpa(spa =>
    {
        if (env.IsDevelopment())
        {
            spa.UseProxyToSpaDevelopmentServer(LocalSpaServerUrl);
        }
        else
        {
            spa.Options.SourcePath = "ClientApp";
            spa.Options.DefaultPageStaticFileOptions = new StaticFileOptions
            {
                FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), "ClientApp/dist/ClientApp/en")),
            };
        }
    });
});

app.Map("/bg", bg =>
{
    bg.UseSpa(spa =>
    {
        spa.Options.SourcePath = "ClientApp";
        spa.Options.DefaultPageStaticFileOptions = new StaticFileOptions
        {
            FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), "ClientApp/dist/ClientApp/bg")),
        };
    });
});

我希望默认路由为 /en:当用户尝试访问“/”时,我希望它重定向到“/en”。 请记住,这是在 Angular 的路由器之外,所以我无法在那里处理。

经过大量挖掘,我找到了正确的方法:

app.Use(async (context, next) =>
{
    if (context.Request.Path.Value == "/")
    {
        // rewrite and continue processing
        context.Request.Path = "/en/";
    }

    await next();
});