【问题标题】:Routing for ASP.NET Core Razor PagesASP.NET Core Razor 页面的路由
【发布时间】:2019-05-22 11:24:01
【问题描述】:

我一直在寻找为 ASP.NET Core Razor 页面配置默认路由的方法,但仍然没有成功。这是我的默认路由代码。还有什么我可以做的吗?顺便说一句,这是没有 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";

            });
        }

【问题讨论】:

标签: c# razor asp.net-core


【解决方案1】:

我对问题的理解(来自 cmets 部分),您想要执行以下操作:

  1. 向自定义剃须刀页面添加路由。
  2. 更改登录页面重定向。

您可以执行以下操作以将自定义路由添加到剃须刀页面:

//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);

要更改登录页面,您需要这样做:

  1. 将 [Authorize] 添加到您想要授权访问的页面。或关注Microsoft guides

如果您有 Microsoft 脚手架式身份页面,例如:

services.AddDefaultIdentity<IdentityUser>()
   .AddEntityFrameworkStores<ApplicationDbContext>();

您需要用自己的Identity 替换它(除非有办法覆盖默认值)。因为默认会将登录路径设置为:/Identity/Account/Login

在实现自己的身份之后,您可以设置 cookie 选项。

services.ConfigureApplicationCookie(options => {
   options.LoginPath = "/Bank/Login";
});

这些步骤对我有用。如果您坚持使用默认身份,您可以添加CookieAuthenticationEvents,然后实现您自己的OnRedirectToLogin

编辑:这里有一些有用的网站:

  1. Razor pages configuration
  2. Configure ASP.NET Core Identity
  3. Customising-identity

【讨论】:

  • 感谢您提供此信息。实际上,目前,我只是在使用旧学校课程。尚未实现 asp.net 核心标识。
  • 我试过你的代码。我认为它确实工作了一半。现在我要弄清楚在哪里设置我的登录页面。 services.AddMvc().AddRazorPagesOptions(options => { options.RootDirectory = "/Pages/Bank/"; }).SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
  • AmbiguousMatchException:请求匹配多个端点。匹配项: Page: /Index Page: //Index
  • 这可能是因为您在pages folder 中有一个页面,它与pages folder 中的文件夹同名,带有Index.cshtml。
【解决方案2】:

如果您在此处进行了更改

 defaults: new { controller = "Bank", action = "Login" });

您应该知道需要在控制器中定义什么样的操作。通常保持索引或默认值,如果您的要求是每次应用启动时都重定向到登录,那么您可以在您的操作中进行设置。

例如重定向登录操作

// 
// GET: /Account/Login 
 [AllowAnonymous] 
 public ActionResult Login(string returnUrl) 
 { 
     ViewBag.ReturnUrl = returnUrl; 
    return View(); 
  } 

【讨论】:

  • 如果你想自动登录,确定使用授权属性和设置认证中间件?
【解决方案3】:

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}");
    });
}

【讨论】:

    猜你喜欢
    • 2020-05-06
    • 2020-05-06
    • 2018-11-14
    • 1970-01-01
    • 1970-01-01
    • 2020-05-25
    • 2018-01-31
    • 2020-02-25
    • 2018-12-04
    相关资源
    最近更新 更多