【发布时间】:2022-01-18 10:41:13
【问题描述】:
在运行 .NET6 的 Blazor 服务器应用程序中,我使用 app.UseBasePath("/app") 将子目录添加到应用程序的基本 URL。所以它从 .com 变为 .com/app。起初这似乎工作正常。应用程序启动,并且 URL 按预期位于 /app 处。但是,它不适用于“区域”文件夹中包含的页面。我正在使用 Microsoft Identity 进行身份验证和授权。我已经搭建了几个 Identity 页面,包括 Login.cshtml、Register.cshtml 等。这些搭建的页面不是 Blazor 组件,因此 Visual Studio 将它们全部放在名为 Identity 的 Area 文件夹中。请参阅随附的屏幕截图。
例如,主页 Blazor 组件位于 /Pages/Index.razor 中。通常可以通过 http://localhost:5000/ 访问此页面。使用 app.UseBasePath("/app") 更改基本路径后,它可以通过 http://localhost:5000/app 访问。这就是我想要的,它适用于所有 Blazor 组件。但是,它不适用于 /Areas 文件夹中的任何页面。注册页面位于 /Areas/Identity/Account/Register.cshtml。按照惯例,此页面通常可通过 http://localhost:5000/Identity/Account/Register 访问。更改基本路径后,我预计它会在 http://localhost:5000/app/Identity/Account/Register 加载,但指向 /Identity/Account/Register 的链接仍然指向不包含 /app 的旧网址。
这是我的 Startup.cs
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
var identityConnectionString = builder.Configuration.GetConnectionString("IdentityConnection");
builder.Services.AddDbContext<ApplicationDbContext>(options =>
options.UseMySql(identityConnectionString, ServerVersion.AutoDetect(identityConnectionString)));
builder.Services.AddDatabaseDeveloperPageExceptionFilter();
builder.Services.AddDefaultIdentity<ApplicationUser>(options => options.SignIn.RequireConfirmedAccount = true)
.AddRoles<IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>();
builder.Services.AddRazorPages();
builder.Services.AddServerSideBlazor();
builder.Services.AddScoped<AuthenticationStateProvider, RevalidatingIdentityAuthenticationStateProvider<ApplicationUser>>();
builder.Services.AddScoped<IIdentityService, IdentityService>();
builder.Services.AddSingleton<WeatherForecastService>();
var app = builder.Build();
app.UsePathBase("/app");
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseMigrationsEndPoint();
}
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(); // NGINX will handle HTTPS redirection
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();
app.MapBlazorHub();
app.MapFallbackToPage("/_Host");
app.Run();
解决方案中是否还有其他文件需要为这些区域配置基本路径?
【问题讨论】: