mhsimkin
mhsimkin

Reputation: 321

How can a Blazor server side app redirect from http://hostname to http://hostname/route1 on load?

For several of the sites I have built using MVC/TypeScript or MVC/Angular I have needed to redirect the user immediately from http://hostname to http://hostname/route1. I am trying to figure out how to implement the same functionality with Blazor Server Side.

I am aware that this can be done using the IIS URL Rewrite engine, however, it has not been determined if the application will be hosted under IIS, self-hosted on Windows, or hosted on Linux.

Is this possible?

Upvotes: 0

Views: 600

Answers (2)

mhsimkin
mhsimkin

Reputation: 321

I changed MainLayout.razor to override the OnInitialized method.

@inject NavigationManager MyNavigationManager
@code
{
    protected override void OnInitialized()
    {
        var currentUri = new Uri(MyNavigationManager.Uri);
        if (currentUri.AbsolutePath == "/")
                MyNavigationManager.NavigateTo("route1");
    }
}

Upvotes: 0

dani herrera
dani herrera

Reputation: 51715

You can do a redirect on OnInitialize on your root page (maybe index.razor )

@page "/"
@inject NavigationManager MyNavigationManager
@code
{
    protected override void OnInitialized()
    {
        MyNavigationManager.NavigateTo("route1");
    }
}

Learn more about navigationManager at NavigationManager cheatsheet

Upvotes: 1

Related Questions