Search code examples
url-routing.net-core-3.0blazor-server-sidematblazor

MatBlazor Button Link is not parsed by routing


I'm using MatBlazor for my website and I've implemented Google Authentication using this great blog: Google Authentication in Server-Side Blazor

I want a login button (MatButton) in my MatAppBar.

The original code has a link: <a class="ml-md-auto btn btn-primary" href="/Login" target="_top">Login</a>.
This link works. I'm redirected to my OnGetAsync of my LoginModel. But it is not matching my UI style.

This button does go to the right page, but my OnGetAsync of my LoginModel is not triggered and only the default Sorry, there's nothing at this address. is shown.
<MatButton Class="mat" Outlined="true" Icon="Google" Label="Inloggen" Link="/Login"></MatButton>

I think I need to tweak my routing, but can't find how.

Update:
My Login.cshtml.cs:

[AllowAnonymous]
public class LoginModel : PageModel
{
    public IActionResult OnGetAsync(string returnUrl = null)
    {
        string provider = "Google";
        // Request a redirect to the external login provider.
        var authenticationProperties = new AuthenticationProperties
        {
            RedirectUri = Url.Page("./Login",
            pageHandler: "Callback",
            values: new { returnUrl }),
        };
        return new ChallengeResult(provider, authenticationProperties);
    }
}

My Startup.cs:

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Error");
            // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
            app.UseHsts();
        }

        app.UseHttpsRedirection();
        app.UseStaticFiles();

        app.UseRouting();

        app.UseCookiePolicy();
        app.UseAuthentication();
        app.UseAuthorization();

        app.UseEmbeddedBlazorContent(typeof(MatBlazor.BaseMatComponent).Assembly);

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapBlazorHub();
            endpoints.MapFallbackToPage("/_Host");
        });
    }

Solution

  • I had a very similar issue and used the navigationmanager option listed above but had to pass the ForceLoad parameter as "true"

     void LoginClick()
                {
                    navigationManager.NavigateTo("/login",true);
                }