繁体   English   中英

.net 5 razor 页面路由

[英].net 5 razor page routing

在我们的一个项目(.NET 5)中,Razor 页面路由的行为有点奇怪。 中间件管道没有什么特别之处:

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:

身份页面

当我们在ProjectRoot/Areas/Identity/Pages/Account/Login.cshtml.cs中的public async Task OnGetAsync(string? returnUrl = null)上设置断点并运行项目时,会发生奇怪的事情。

每次我们想在浏览器中访问https://localhost:5001/时,都会触发Login.cshtml.cs中的断点。 甚至在ProjectRoot/Pages/Index.cshtml.cs之前。

我们怎样才能找到它为什么会这样呢? 为什么应用程序在Index.cshtml.cs之前路由到Login.cshtml.cs

在登录断点调试时,上下文值为:

Request.Path = "/"
Request.RouteValues = { ["page"] = "/Index" }

我认为您每次都被重定向到登录,因为您没有登录。这可以解释这种情况。

然后你在调试'登录',而你认为你正在调试'索引',这可能会非常令人困惑。

这也发生在我身上,您认为您正在调试一个请求,但事实证明您正在调试另一个请求(在我的情况下,我实际上是在调试 VS 在我不知情的情况下发送的对“favicon.ico”的请求)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM