使用 Razor 页面更改 ASP.Net Core 2.1 中的主页
Changing home page in ASP.Net Core 2.1 with Razor pages
我正在使用 Razor 页面开发 ASP.Net Core 2.1 网站,这是我第一次使用 Razor 页面。但我想做的是更改主页或登录页面。因此,如果用户未登录,该站点应重定向到 Areas 文件夹中的 /Account/Login 页面,但如果用户已登录,则应转到页面文件夹中名为 DataManagement 的页面,如下所示。
我已经缝制了身份,并且在配置服务中尝试了类似下面的操作:
services.AddMvc().AddRazorPagesOptions(options =>
{
options.Conventions.AddAreaPageRoute("Identity", "/Account/Login", "");
}).SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
并且在配置方法中:
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
无果。
编辑
我的 StartUp.cs
public class 启动
{
public Startup(IConfiguration配置)
{
配置=配置;
}
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.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("Connection")));
services.AddIdentity<ApplicationUser, ApplicationRole>(
options => options.Stores.MaxLengthForKeys = 128)
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultUI()
.AddDefaultTokenProviders()
.AddEntityFrameworkStores<ApplicationDbContext>();
services.AddMvc().AddRazorPagesOptions(opts =>
{
opts.Conventions.AddPageRoute("/DataManagement", "/");
opts.Conventions.AddPageRoute("/DataManagement", "home");
opts.Conventions.AddPageRoute("/DataManagement", "index");
}).SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ApplicationDbContext context, RoleManager<ApplicationRole> roleManager, UserManager<ApplicationUser> userManager, IServiceProvider serviceProvider)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseDatabaseErrorPage();
}
else
{
app.UseExceptionHandler("/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseCookiePolicy();
app.UseAuthentication();
app.UseMvc();
}
最简单的方法就是在 DataManagement.cshtml.cs
文件中使用 [Authorize]
属性。
[Authorize]
public class DataManagementModel : PageModel
{
public void OnGet()
{
}
}
只需像往常一样在 Startup.cs
中配置您的默认主页:
services.AddMvc().AddRazorPagesOptions(opts =>
{
opts.Conventions.AddPageRoute("/DataManagement", "/");
opts.Conventions.AddPageRoute("/DataManagement", "home");
opts.Conventions.AddPageRoute("/DataManagement", "index");
opts.Conventions.AddAreaPageRoute("Account", "/Login", "/Account/Login");
}).SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.ConfigureApplicationCookie(options =>
{
options.LoginPath = $"/Account/Login";
});
并在配置中:
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
然后删除 Index.cshtml
我认为在这种情况下你需要像上面那样定义自定义登录,取自here
我正在使用 Razor 页面开发 ASP.Net Core 2.1 网站,这是我第一次使用 Razor 页面。但我想做的是更改主页或登录页面。因此,如果用户未登录,该站点应重定向到 Areas 文件夹中的 /Account/Login 页面,但如果用户已登录,则应转到页面文件夹中名为 DataManagement 的页面,如下所示。
我已经缝制了身份,并且在配置服务中尝试了类似下面的操作:
services.AddMvc().AddRazorPagesOptions(options =>
{
options.Conventions.AddAreaPageRoute("Identity", "/Account/Login", "");
}).SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
并且在配置方法中:
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
无果。
编辑 我的 StartUp.cs
public class 启动 { public Startup(IConfiguration配置) { 配置=配置; }
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.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("Connection")));
services.AddIdentity<ApplicationUser, ApplicationRole>(
options => options.Stores.MaxLengthForKeys = 128)
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultUI()
.AddDefaultTokenProviders()
.AddEntityFrameworkStores<ApplicationDbContext>();
services.AddMvc().AddRazorPagesOptions(opts =>
{
opts.Conventions.AddPageRoute("/DataManagement", "/");
opts.Conventions.AddPageRoute("/DataManagement", "home");
opts.Conventions.AddPageRoute("/DataManagement", "index");
}).SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ApplicationDbContext context, RoleManager<ApplicationRole> roleManager, UserManager<ApplicationUser> userManager, IServiceProvider serviceProvider)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseDatabaseErrorPage();
}
else
{
app.UseExceptionHandler("/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseCookiePolicy();
app.UseAuthentication();
app.UseMvc();
}
最简单的方法就是在 DataManagement.cshtml.cs
文件中使用 [Authorize]
属性。
[Authorize]
public class DataManagementModel : PageModel
{
public void OnGet()
{
}
}
只需像往常一样在 Startup.cs
中配置您的默认主页:
services.AddMvc().AddRazorPagesOptions(opts =>
{
opts.Conventions.AddPageRoute("/DataManagement", "/");
opts.Conventions.AddPageRoute("/DataManagement", "home");
opts.Conventions.AddPageRoute("/DataManagement", "index");
opts.Conventions.AddAreaPageRoute("Account", "/Login", "/Account/Login");
}).SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.ConfigureApplicationCookie(options =>
{
options.LoginPath = $"/Account/Login";
});
并在配置中:
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
然后删除 Index.cshtml
我认为在这种情况下你需要像上面那样定义自定义登录,取自here