I am programming a website in the .NET framwork Blazor (.NET 8.0). I wanted to add an login via Active Directory and after this I got the following error:
System.InvalidOperationException: Cannot find the fallback endpoint specified by route values: { page: /_Host, area: }.
at Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.DynamicPageEndpointMatcherPolicy.ApplyAsync(HttpContext httpContext, CandidateSet candidates)
at Microsoft.AspNetCore.Routing.Matching.DfaMatcher.SelectEndpointWithPoliciesAsync(HttpContext httpContext, IEndpointSelectorPolicy[] policies, CandidateSet candidateSet)
at Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware.<Invoke>g__AwaitMatch|10_1(EndpointRoutingMiddleware middleware, HttpContext httpContext, Task matchTask)
at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddlewareImpl.Invoke(HttpContext context)
After long searching through the internet I found a solution for the problem but it didnt worked for me. The solution was the following one:
<PropertyGroup>
<UseRazorSourceGenerator>false</UseRazorSourceGenerator>
</PropertyGroup>`
This should be added to the project.csproj. Maybe someone can tell me what my mistake is and help me to solve it.
_Host.cshtml:
@page "/"
@namespace Berhub
@using Berhub.Pages
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>BERHub</title>
<base href="~/" />
<link href="css/bootstrap/bootstrap.min.css" rel="stylesheet" />
<link href="css/app.css" rel="stylesheet" />
</head>
<body>
<app>
<component type="typeof(App)" render-mode="ServerPrerendered" />
</app>
<script src="_framework/blazor.server.js"></script>
</body>
</html>
Program.cs:
using Berhub;
using Berhub.Models;
using berhub.Components;
using Dapper;
using Berhub.DataAccessLayer.DataAccessLayer;
using Berhub.DataAccessLayer.SqlDataAccess;
using Radzen;
using Radzen.Blazor;
using Microsoft.Extensions.DependencyInjection;
using Berhub.DataAccessLayer.Repositories;
using Microsoft.AspNetCore.Authentication.Negotiate;
using Microsoft.AspNetCore.Authorization;
using Microsoft.Extensions.Configuration;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddRazorPages();
builder.Services.AddServerSideBlazor();
builder.Services.AddRazorComponents()
.AddInteractiveServerComponents();
builder.Services.AddRadzenComponents();
builder.Services.AddScoped<IWindowsServerData, WindowsServerData>();
builder.Services.AddScoped<ISqlDataAccess, SqlDataAccess>();
builder.Services.AddScoped<IHostnameData<HostnameModel>, HostnameData>();
builder.Services.AddScoped<IAppointment<Appointment>, Berhub.DataAccessLayer.DataAccessLayer.AppointmentData>();
var configuration = builder.Configuration;
builder.Services.AddAuthentication(NegotiateDefaults.AuthenticationScheme).AddNegotiate(); // Windows authentication may not be applied with Kestrel without this line
builder.Services.AddAuthorization(
options =>{
options.AddPolicy("KeyUsers", policy =>
policy.RequireRole(configuration.GetSection("ActiveDirectory")["KeyUsers"]));
});
/**builder.Services.AddRazorPages(options =>
{
options.RootDirectory = "/Components";
});**/
builder.Services.AddServerSideBlazor();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error", createScopeForErrors: true);
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseAntiforgery();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.MapRazorPages();
app.MapBlazorHub();
app.MapFallbackToPage("/_Host"); // Pfad zur _Host.cshtml anpassen
app.Run();
index.razor (login):
@page "/"
@attribute [StreamRendering]
<PageTitle>Home</PageTitle>
<AuthorizedView Policy="KeyUsers">
<Authorized>
</Authorized>
<NotAuthorized>
<link rel="stylesheet" href="Login.razor.css" />
<div class= "container">
<form method = "post">
<fieldset>
<legend>Anmelden</legend>
<div class="nutznername">
<label for="username">Benutzername: </label>
<input type="text" name="username" value=""/>
</div>
<div class="passwort">
<label for="password">Passwort: </label>
<input type="password" name="password">
</div>
<div class="bestätigen">
<label> </label>
<input type="submit" value="Submit" class="submit" />
</div>
</fieldset>
</form>
</div>
</NotAuthorized>
</AuthorizedView>
You removed the codes that configured the RootDirectory of razor pages:
/**builder.Services.AddRazorPages(options =>
{
options.RootDirectory = "/Components";
});**/
According to the codes above,your _host.cshtml should be in Components
folder.Try recover the codes and put your _host.cshtml in the right place
If you still get the error,please show us the structure of your project
A minimal example on myside:
Program.cs:
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddRazorComponents()
.AddInteractiveServerComponents();
builder.Services.AddRazorPages(options =>
{
options.RootDirectory = "/Components/Pages";
});
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error", createScopeForErrors: true);
// 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.UseAntiforgery();
app.MapRazorComponents<App>()
.AddInteractiveServerRenderMode();
app.MapFallbackToPage("/_Host");
app.Run();
Project structure:
_Host.cshtml:
@page "/"
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>BERHub</title>
<base href="~/" />
<link href="css/bootstrap/bootstrap.min.css" rel="stylesheet" />
<link href="css/app.css" rel="stylesheet" />
</head>
<body>
FallBack Page
</body>
</html>
It would go to the fall backpage when it can't find the component for current route: