[英]Routing for ASP.NET Core Razor Pages
我一直在寻找为ASP.NET Core Razor Pages配置默认路由的方法,但仍然没有成功。 这是我的默认路由代码。 我还能做什么? 顺便说一下,这是没有MVC的纯Razor页面。
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.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
services.AddDbContext<AppDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("AppDbContext")));
services.AddMvc()
.AddRazorPagesOptions(options =>
{
options.RootDirectory = "/Bank";
});
}
我对问题的理解(来自评论部分),您想要执行以下操作:
您可以执行以下操作将自定义路由添加到剃须刀页面:
//This should be in the very end.
services.AddMvc().AddRazorPagesOptions(options =>
{
//just to respect Microsoft way, it is better to have Pages folder
//to contains your folders.
options.RootDirectory = "/CustomFolder/Pages";
}).SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
要更改登录页面,您需要执行以下操作:
如果您有用于身份的Microsoft支架式页面,例如:
services.AddDefaultIdentity<IdentityUser>()
.AddEntityFrameworkStores<ApplicationDbContext>();
您需要用自己的Identity
替换它(除非有一种方法可以覆盖默认值)。 因为默认设置会将登录路径设置为: /Identity/Account/Login
。
在实现自己的身份之后,您可以设置cookie选项。
services.ConfigureApplicationCookie(options => {
options.LoginPath = "/Bank/Login";
});
这些步骤对我有用。 如果您坚持使用默认的Identity,则可以添加CookieAuthenticationEvents
,然后实现自己的OnRedirectToLogin
。
编辑:这是一些有用的站点:
如果您在这里进行了更改
defaults: new { controller = "Bank", action = "Login" });
您应该知道需要在控制器中定义哪种操作。 通常保留索引或默认值,如果您的要求是每次启动应用程序时重定向到登录名,则可以进行操作。
例如重定向登录操作
//
// GET: /Account/Login
[AllowAnonymous]
public ActionResult Login(string returnUrl)
{
ViewBag.ReturnUrl = returnUrl;
return View();
}
ASP.NET Core MVC本身使用路由中间件。 如果您不想使用MVC中间件,则可以直接使用它。
public void ConfigureServices(IServiceCollection services)
{
services.AddRouting();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseRouter(cfg =>
{
cfg.MapRoute("default", "segmentA/{segmentB}");
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.