Can't get razor page route to work using anchor tag helper

三世轮回 提交于 2020-03-04 18:48:38

问题


Background

This is a follow on issue to an initial question I asked around asp.net core v3.1 razor page routes.

I added the following to the startup as suggested in the solution here which allowed me to route to the razor page in both ways I wanted:

services.AddMvc()
        .AddRazorPagesOptions(options => 
                 options.Conventions.AddAreaPageRoute("Identity", "/account/signupandapply", "/identity/account/signup")
        );
/identity/account/signup
/identity/account/signupandapply/<fooapplyid>

Issue

I'm struggling to use the anchor tag helper to respect the path route /identity/account/signupandapply/<fooapplyid>. The href shows using the parameter instead /identity/account/signupandapply?fooapplyid=<fooapplyid>. This is my anchor tag helper markup:

<a asp-area="identity" asp-page="/account/signupandapply" asp-route-fooapplyid="@Model.FooApplyId">Apply here</a>

Currently, I'm manually setting the href but would like to know how to get this working using the tag helper.

<a href="/identity/account/signupandapply/@Model.FooApplyId">Apply here</a>

Note: The anchor markup is being added to a view outside of the identity area.

Attempted

I've tried adding the below which shows the href as I want. I.e /identity/account/signupandapply/<fooapplyid>

.AddAreaPageRoute("Identity", "/account/signupandapply", "/identity/account/signupandapply/{applyid}")

But I then get an error when I go to the page, which makes sense

AmbiguousMatchException: The request matched multiple endpoints. Matches: /Account/SignUpAndApply /Account/SignUpAndApply

Neither of these work, the href still shows as ?applyid=:

.AddAreaPageRoute("Identity", "/account/signupandapply?applyid=", "/identity/account/signupandapply/{applyid}")

.AddAreaPageRoute("Identity", "/account/signupandapply/{applyid?}", "/identity/account/signupandapply/{applyid}")

Question

Is there a further startup route that needs defining or how do I get the anchor tag helper to respect the path route?


回答1:


AddAreaPageRoute("Identity", "/account/signupandapply", "/identity/account/signup")

To quote the docs on this (emphasis mine):

conventions.AddAreaPageRoute(string areaName, string pageName, string route)

Adds the specified route to the page at the specified pageName located in the specified area.

The page can be routed via route in addition to the default set of path based routes. All links generated for this page will use the specified route.

So this convention effectively tells the routing to use the /identity/acount/signup route from now on to generate all links, while accepting incoming routes to hit both these routes.

Unfortunately, I don’t think that you can control this any better using the page conventions. Razor pages do not really fit a routing model where you are having different route to reach the same page. After all, Razor pages are page-centric instead of route-centric like MVC actions.

So if this is a hard requirement for you, you might want to look at using a controller for this one use-case instead that renders the same view. For what it’s worth, controllers do mix nicely with an application that is otherwise using Razor pages.



来源:https://stackoverflow.com/questions/60265572/cant-get-razor-page-route-to-work-using-anchor-tag-helper

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