On one of our projects (.NET 5) the Razor Page routing behaves kind of strange. There is nothing extraordinary in middleware pipeline:
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(); });
}
And even services contains nothing extra:
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
);
}
We have scaffolded some identity RazorPages:
And curious thing happens when we put a breakpoint on public async Task OnGetAsync(string? returnUrl = null)
in ProjectRoot/Areas/Identity/Pages/Account/Login.cshtml.cs
and run the project.
Everytime we want to access https://localhost:5001/
in browser, breakpoint in Login.cshtml.cs
is triggered. Even before ProjectRoot/Pages/Index.cshtml.cs
.
How can we find why it behaves like this? Why the application is routing to Login.cshtml.cs
before Index.cshtml.cs
?
When debugging on breakpoint in login, then context values are:
Request.Path = "/"
Request.RouteValues = { ["page"] = "/Index" }
CodePudding user response:
I think you're being redirected to login every time because you're not logged in. That could explain the situation.
Then you're debugging 'Login' while you think you're debugging 'Index', which can be VERY confusing.
This has also happened to me, you think you're debugging a request, but it turns out you're debugging another request (in my case I was actually debugging a request to 'favicon.ico' which VS sent without my knowledge).