0

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? If so, what is the best practice approach for this implementation?

Thanks

mhsimkin
  • 311
  • 4
  • 15
  • You could redirect on the index components OnInitialized() method by calling an injected NavigationManager to redirect to your desired location. – olabacker Jan 27 '21 at 20:44
  • Thus you're making the Index component useless as whenever the user tries to reach the Index page, he's automatically redirected to the desired location. This solution is rather impractical, no ? – enet Jan 27 '21 at 23:45

2 Answers2

1

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

dani herrera
  • 48,760
  • 8
  • 117
  • 177
0

Thank you everyone. You provided a starting point.

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");
    }
}
mhsimkin
  • 311
  • 4
  • 15