无法使用定位标记帮助程序来获取剃须刀页面路由 [英] Can't get razor page route to work using anchor tag helper

查看:108
本文介绍了无法使用定位标记帮助程序来获取剃须刀页面路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景

这是对初始问题的后续报道>我询问了asp.net核心v3.1剃须刀页面路由.

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>

问题

我正在努力使用定位标记帮助程序来遵守路径路由/identity/account/signupandapply/<fooapplyid>. href显示使用参数代替/identity/account/signupandapply?fooapplyid=<fooapplyid>.这是我的锚标记助手标记:

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>

当前,我正在手动设置href,但是想知道如何使用标签助手来实现此功能.

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.

尝试

我尝试添加以下显示所需的href的内容.即/identity/account/signupandapply/<fooapplyid>

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

以上两项都不起作用,href仍显示为?applyid =:

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

问题

是否还有一条需要定义的启动路线,或者如何使锚标记帮助程序尊重路径路线?

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

推荐答案

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

要引用

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

将指定的route添加到位于指定区域中的指定pageName的页面上.

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

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

除了默认的基于路径的路由集之外,还可以通过route路由页面. 为此页面生成的所有链接将使用指定的路由.

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.

因此,该约定有效地告诉路由从现在开始使用/identity/acount/signup路由来生成所有链接,同时接受进入的路由以同时击中这两个路由.

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.

不幸的是,我认为您无法使用页面约定更好地控制此操作.剃刀页面并不真正适合您要使用不同的路由才能到达同一页面的路由模型.毕竟,Razor页面是以页面为中心的,而不是像MVC操作那样以路由为中心的.

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.

因此,如果这对您来说是硬性要求,那么您可能希望针对这种用例使用控制器,而不是呈现相同的视图.就其价值而言,控制器确实可以与原本使用Razor页面的应用程序完美地结合在一起.

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.

这篇关于无法使用定位标记帮助程序来获取剃须刀页面路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆