Multiple routes to the same razor page

穿精又带淫゛_ 提交于 2021-01-29 22:32:58

问题


Background

I've got a razor page in my Asp.Net Core 3.1 website called SignupAndApply. It's effectively a copy and paste of the register identity page but:

  • With some additional fields
  • Allows an optional applyid to be passed as part of the route
  • If I pass an applyId as part of the route, I make a couple of label changes to the page and redirect the user somewhere else

I want to create two routes for this page:

/identity/account/signupandapply/{applyId?} 'when the user is applying and signing up
/identity/account/signup 'when the user is just signing up

I've added the following to the top of the page:

@page "{applyId?}"

And set the applyId as an optional parameter on the OnGetAsync method:

public async Task OnGetAsync(string returnUrl = null, Guid? applyId = null)

Issue

The route with the applyId is working but the /identity/account/signup route isn't.

I've tried adding this to my startup:

services.AddMvc()
        .AddRazorPagesOptions(options => options.Conventions
                             .AddPageRoute("/identity/account/signupandapply", "/identity/account/signup")
                             );

It works if I go to one of these

/identity/account/signupandapply/<fooapplyid>
/identity/account/signupandapply

But not this

/identity/account/signup

Any ideas what I'm doing wrong for what should be adding a simple alternative route?


回答1:


The page(/identity/account/signupandapply) is in area Identity ,so you need to use AddAreaPageRoute instead of AddAreaPage :

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

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

    }); ;


来源:https://stackoverflow.com/questions/60223804/multiple-routes-to-the-same-razor-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!