Search code examples
asp.net-coreblazorblazor-server-side.net-5asp.net-core-5.0

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?


Solution

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