我是 Razor Pages 新手,正在使用个人身份验证在 ASP.NET Core 5 中创建一个小型 Web 应用程序。我希望每当加载我的应用程序时,登录页面都应作为登陆页面打开,然后登录后应重定向到主页。
这是我在 Startup.cs 文件中添加的代码
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddInfrastructure(Configuration);
services.AddMvc().AddRazorPagesOptions(options =>
{
options.Conventions.AddAreaPageRoute("Identity", "/Account/Login", "");
});
services.AddRazorPages();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
}
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
});
}
}
问题是成功登录并注册后,应用程序没有重定向到主页。它保留在登录页面上。
有人可以指导我哪里出错了吗?
添加到 Razor 页面
@page "/Login"
[AllowAnonymous]
public class IndexModel : PageModel