I am relatively new to blazor. I have created a razor page with login functionality, that attaches a cookie with a redirect, when login is ok.
My test flow: When I run my app, i'll need to show 'index.cshtml' Here I 'log in', which make a form post to the CS code, that: return Redirect("/secret/home"); SecretHome.razor is a blazor page that shows users name, so i know I'm logged in. I't located on: @page "/secret/home" This page also have a: navManager.NavigateTo("/secret/todo/NavManager"); Last page is: @page "/secret/todo/{id}"
The error: Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[1] An unhandled exception has occurred while executing the request. Microsoft.AspNetCore.Routing.Matching.AmbiguousMatchException: The request matched multiple endpoints. Matches:
Blazor initializers
Fallback {*path:nonfile}
at Microsoft.AspNetCore.Routing.Matching.DefaultEndpointSelector.ReportAmbiguity(Span`1 candidateState)
at Microsoft.AspNetCore.Routing.Matching.DefaultEndpointSelector.ProcessFinalCandidates(HttpContext httpContext, Span`1 candidateState)
at Microsoft.AspNetCore.Routing.Matching.DefaultEndpointSelector.Select(HttpContext httpContext, Span`1 candidateState)
at Microsoft.AspNetCore.Routing.Matching.DfaMatcher.MatchAsync(HttpContext httpContext)
at Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware.Invoke(HttpContext httpContext)
at Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddlewareImpl.Invoke(HttpContext context)
My program.cs
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddRazorPages();
builder.Services.AddRazorComponents().AddInteractiveServerComponents();
builder.Services.AddHttpContextAccessor();
builder.Services.AddHttpContextAccessor();
builder.Services.AddTransient<FakeAuthRepo>();
builder.Services.AddSingleton<UserService>();
builder.Services.AddSingleton<IAuthRepo, FakeAuthRepo>();
var app = builder.Build();
app.UseStaticFiles();
app.UseRouting();
app.UseHttpsRedirection();
app.UseAntifery();
app.MapControllers();
app.MapBlazorHub();
app.MapRazorPages();
app.MapRazorComponents<App>().AddInteractiveServerRenderMode();
app.MapFallbackToFile("index.html");
app.Run();
As I said, it seems like everything works fine, but I get this error and I suspect I at somehow would come over an issue due to that error, so I really would like it to go away.
I hope someone can help. Thanks.
I am relatively new to blazor. I have created a razor page with login functionality, that attaches a cookie with a redirect, when login is ok.
My test flow: When I run my app, i'll need to show 'index.cshtml' Here I 'log in', which make a form post to the CS code, that: return Redirect("/secret/home"); SecretHome.razor is a blazor page that shows users name, so i know I'm logged in. I't located on: @page "/secret/home" This page also have a: navManager.NavigateTo("/secret/todo/NavManager"); Last page is: @page "/secret/todo/{id}"
The error: Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[1] An unhandled exception has occurred while executing the request. Microsoft.AspNetCore.Routing.Matching.AmbiguousMatchException: The request matched multiple endpoints. Matches:
Blazor initializers
Fallback {*path:nonfile}
at Microsoft.AspNetCore.Routing.Matching.DefaultEndpointSelector.ReportAmbiguity(Span`1 candidateState)
at Microsoft.AspNetCore.Routing.Matching.DefaultEndpointSelector.ProcessFinalCandidates(HttpContext httpContext, Span`1 candidateState)
at Microsoft.AspNetCore.Routing.Matching.DefaultEndpointSelector.Select(HttpContext httpContext, Span`1 candidateState)
at Microsoft.AspNetCore.Routing.Matching.DfaMatcher.MatchAsync(HttpContext httpContext)
at Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware.Invoke(HttpContext httpContext)
at Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddlewareImpl.Invoke(HttpContext context)
My program.cs
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddRazorPages();
builder.Services.AddRazorComponents().AddInteractiveServerComponents();
builder.Services.AddHttpContextAccessor();
builder.Services.AddHttpContextAccessor();
builder.Services.AddTransient<FakeAuthRepo>();
builder.Services.AddSingleton<UserService>();
builder.Services.AddSingleton<IAuthRepo, FakeAuthRepo>();
var app = builder.Build();
app.UseStaticFiles();
app.UseRouting();
app.UseHttpsRedirection();
app.UseAntifery();
app.MapControllers();
app.MapBlazorHub();
app.MapRazorPages();
app.MapRazorComponents<App>().AddInteractiveServerRenderMode();
app.MapFallbackToFile("index.html");
app.Run();
As I said, it seems like everything works fine, but I get this error and I suspect I at somehow would come over an issue due to that error, so I really would like it to go away.
I hope someone can help. Thanks.
Share Improve this question edited Apr 1 at 7:35 Tiny Wang 16.4k2 gold badges18 silver badges38 bronze badges asked Mar 31 at 12:57 Benny JørgensenBenny Jørgensen 351 gold badge1 silver badge6 bronze badges 4- 1 Why still use razor-pages? Version 8 added Static Serverside Rendering, that delivers the same hosting model using Blazor components. – Henk Holterman Commented Mar 31 at 14:19
- All I need is a full page load when form is submitted. If that can be done without an old razor page, then it's ok with me. – Benny Jørgensen Commented Apr 1 at 5:25
- "it seems like everything works fine" - so the app works and you only see this error in the debugger? Or is there an error for the user? – Henk Holterman Commented Apr 1 at 9:21
- I would advise to create a Blazor 9 app from the starter template with "Individual accounts". Study (and copy) the Login page and the RedirectManager code. – Henk Holterman Commented Apr 1 at 9:24
1 Answer
Reset to default 0You could use EditForm
in SSR (static server render, similar to http request, HttpContext is available) rendermode which form submit will trigger a reload.
@page "/login"
<EditForm Model="loginUser" Formname="form1" OnSubmit="IncrementCount">
Name:<InputText @bind-value="loginUser.Name"/> <br />
Pwd: <InputText @bind-value="loginUser.Pwd" /> <br />
<button type="submit">submit</button>
</EditForm>
@code {
[CascadingParameter]
HttpContext? httpContext { get; set; }
[SupplyParameterFromForm]
private LoginUser loginUser { get; set; }
protected override void OnInitialized() => loginUser ??= new();
private void IncrementCount()
{
//set cookie example.
httpContext.Response.Cookies.Append(loginUser.Name,loginUser.Pwd);
}
private class LoginUser{
public string Name { get; set; }
public string Pwd { get; set; }
}
}
Just note that you need per page rendermode to set a null(SSR) to rendermode. If you choose globally , set page rendermode dynamically based on route. https://learn.microsoft/en-us/aspnet/core/blazor/components/render-modes?view=aspnetcore-9.0#set-the-render-mode-by-component-instance
Another solution you could try simplely get/set/delete browser cookie storage directly using JS interop. This way you don't have to use SSR or HttpContext.