无法使用锚标记助手使剃刀页面路由正常工作 [英] Can't get razor page route to work using anchor tag helper

查看:30
本文介绍了无法使用锚标记助手使剃刀页面路由正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景

这是对初始问题的后续问题 我询问了asp.net core v3.1 razor 页面路由.

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

我按照解决方案此处中的建议将以下内容添加到启动中,这使我可以路由到 razor 页面我想要的两种方式:

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/.href 显示使用参数代替 /identity/account/signupandapply?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/

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

引述 有关此的文档(重点是我的):

To quote the docs on this (emphasis mine):

将指定的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 页面并不真正适合路由模型,在这种模型中,您可以通过不同的路线到达同一页面.毕竟,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天全站免登陆