RapGeneral
RapGeneral

Reputation: 13

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

I am using multi-language angular app in my project as follows:

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

I want the default route to be the /en one: when a user attempts to acces '/' I want it to redirect to '/en'. Keep in mind this is outside of Angular's router, so I cannot handle this there.

Upvotes: 0

Views: 659

Answers (1)

RapGeneral
RapGeneral

Reputation: 13

After digging a lot, I found the correct way:

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

    await next();
});

Upvotes: 1

Related Questions