在我們的一個專案 (.NET 5) 中,Razor Page 路由的行為有點奇怪。中間件管道沒有什么特別之處:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseMigrationsEndPoint();
}
else
{
app.UseExceptionHandler("/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints => { endpoints.MapRazorPages(); });
}
甚至服務也不包含任何額外內容:
public void ConfigureServices(IServiceCollection services)
{
string connectionString = Configuration.GetConnectionString("DefaultConnection");
// for scoped services (mainly for identity)
services.AddDbContext<DbEntities>(options =>
options.UseSqlServer(connectionString));
AddIdentity(services);
services.AddDatabaseDeveloperPageExceptionFilter();
services.AddRazorPages()
.AddRazorRuntimeCompilation();
}
private void AddIdentity(IServiceCollection services)
{
services.AddDefaultIdentity<ApplicationUser>(options =>
{
options.SignIn.RequireConfirmedAccount = true;
options.User.RequireUniqueEmail = true;
options.Password.RequiredLength = 6;
options.Password.RequireNonAlphanumeric = false;
options.Password.RequireDigit = false;
options.Password.RequireLowercase = false;
options.Password.RequireUppercase = false;
options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(5);
options.Lockout.MaxFailedAccessAttempts = 5;
})
.AddRoles<IdentityRole>()
.AddEntityFrameworkStores<DbEntities>();
// this is needed because passwords are stored with old hashes
services.Configure<PasswordHasherOptions>(options =>
options.CompatibilityMode = PasswordHasherCompatibilityMode.IdentityV2
);
}
我們已經搭建了一些身份 RazorPages:

而當我們把一個斷點奇怪的事情發生public async Task OnGetAsync(string? returnUrl = null)在ProjectRoot/Areas/Identity/Pages/Account/Login.cshtml.cs和運行專案。
每次我們想https://localhost:5001/在瀏覽器中訪問時,Login.cshtml.cs都會觸發斷點。甚至之前ProjectRoot/Pages/Index.cshtml.cs。
我們怎樣才能找到它為什么會這樣呢?為什么應用程式路由到Login.cshtml.cs之前Index.cshtml.cs?
在登錄中的斷點上除錯時,背景關系值為:
Request.Path = "/"
Request.RouteValues = { ["page"] = "/Index" }
uj5u.com熱心網友回復:
我認為您每次都被重定向到登錄,因為您沒有登錄。這可以解釋這種情況。
然后你在除錯“登錄”,而你認為??你正在除錯“索引”,這可能非常令人困惑。
這也發生在我身上,你認為你正在除錯一個請求,但事實證明你正在除錯另一個請求(在我的例子中,我實際上是在除錯一個對“favicon.ico”的請求,該請求是在我不知情的情況下發送的)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/371316.html
上一篇:導航屬性不會在兩個表之間獲取資料
