简体   繁体   English

.net 5 razor 页面路由

[英].net 5 razor page routing

On one of our projects (.NET 5) the Razor Page routing behaves kind of strange.在我们的一个项目(.NET 5)中,Razor 页面路由的行为有点奇怪。 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:我们搭建了一些身份 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.当我们在ProjectRoot/Areas/Identity/Pages/Account/Login.cshtml.cs中的public async Task OnGetAsync(string? returnUrl = null)上设置断点并运行项目时,会发生奇怪的事情。

Everytime we want to access https://localhost:5001/ in browser, breakpoint in Login.cshtml.cs is triggered.每次我们想在浏览器中访问https://localhost:5001/时,都会触发Login.cshtml.cs中的断点。 Even before ProjectRoot/Pages/Index.cshtml.cs .甚至在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 ?为什么应用程序在Index.cshtml.cs之前路由到Login.cshtml.cs

When debugging on breakpoint in login, then context values are:在登录断点调试时,上下文值为:

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

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

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

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