到同一个 Razor 页面的多个路由

Nei*_*eil 4 c# asp.net-core-mvc .net-core asp.net-core razor-pages

背景

我的 Asp.Net Core 3.1 网站中有一个名为 SignupAndApply 的剃刀页面。它实际上是注册身份页面的复制和粘贴,但是:

  • 有一些附加字段
  • 允许可选的 applyid 作为路由的一部分传递
  • 如果我传递 applyId 作为路由的一部分,我会对页面进行一些标签更改并将用户重定向到其他地方

我想为此页面创建两条路线:

/identity/account/signupandapply/{applyId?} 'when the user is applying and signing up
/identity/account/signup 'when the user is just signing up
Run Code Online (Sandbox Code Playgroud)

我已将以下内容添加到页面顶部:

@page "{applyId?}"
Run Code Online (Sandbox Code Playgroud)

并将 applyId 设置为 OnGetAsync 方法上的可选参数:

@page "{applyId?}"
Run Code Online (Sandbox Code Playgroud)

问题

带有 applyId 的路由有效,但 /identity/account/signup 路由无效。

我尝试将其添加到我的启动中:

public async Task OnGetAsync(string returnUrl = null, Guid? applyId = null)
Run Code Online (Sandbox Code Playgroud)

如果我去其中之一就可以了

/identity/account/signupandapply/<fooapplyid>
/identity/account/signupandapply
Run Code Online (Sandbox Code Playgroud)

但不是这个

/identity/account/signup
Run Code Online (Sandbox Code Playgroud)

有什么想法我做错了什么应该添加一个简单的替代路线吗?

Nan*_* Yu 5

page( /identity/account/signupandapply) 位于区域中Identity,因此您需要使用AddAreaPageRoute而不是AddAreaPage

services.AddRazorPages().AddRazorPagesOptions(options =>
{

    options.Conventions.AddAreaPageRoute("Identity",
           "/account/signupandapply", "identity/account/signup");

    }); ;
Run Code Online (Sandbox Code Playgroud)