简体   繁体   English

视图中的ASP.NET Core端点路由链接生成

[英]ASP.NET Core Endpoint Routing Link Generation in Views

I'm having an issue while trying to upgrade my ASP.NET Core 2.1 application to 2.2 and use endpoint routing. 尝试将ASP.NET Core 2.1应用程序升级到2.2并使用端点路由时遇到问题。 The link generation works differently and I'm pretty sure it relates entirely to that. 链接生成的工作原理有所不同,我很确定它与之完全相关。

I use link generation within the views to generate links. 我在视图中使用链接生成来生成链接。 In some cases I currently do something like this: 在某些情况下,我目前正在执行以下操作:

let url = "@Url.Action(nameof(NoteController.Category), PortalControllerNames.Note)/" + categoryItem.dataset.categoryId + "/@Model.UserId";

The route for this action is defined as below, which I understand essentially says categoryId and userId are required: 此操作的路由定义如下,据我所知,基本上说categoryIduserId是必需的:

[HttpGet("[controller]/[action]/{categoryId}/{userId}")]

In 2.1 the following is generated from the call to @Url.Action 在2.1中,通过对@Url.Action的调用生成了以下@Url.Action

let url = "/Note/Category/" + categoryItem.dataset.categoryId + "/12345";

Which I can then slot the additional parameter into at runtime via javascript. 然后,我可以在运行时通过javascript将其他参数插入其中。

Under 2.2 with Endpoint Routing enabled the route isn't found and an empty string is returned as per the official docs . 在2.2中启用了端点路由的情况下,找不到路由,并且根据官方文档返回空字符串。

Benefits of using the Url.Action method are things like passing action method names and having the system resolve the route name for them meaning that any change to controller names is carried out via refactoring in VS and any route name changes are automatically carried through the application with no view changes needed. 使用Url.Action方法的好处是传递操作方法名称,并让系统为它们解析路由名称,这意味着对控制器名称的任何更改都是通过VS中的重构进行的,并且任何路由名称的更改都将通过应用程序自动进行无需更改视图。

I understand that the route doesn't exist to be looked up and think this is cause of the issue. 我了解该路由并不存在,因此认为这是问题的原因。 Is a solution to just add a route that will allow generation of the "base" route (without the parameters)? 解决方案是仅添加一条允许生成“基本”路由(不带参数)的路由吗? (I think this would have the side effect that you can now navigate to the route without the parameters being present whereas you'd currently get a 404 when doing so because no route is satisfied?) (我认为这会有副作用,您现在可以导航到该路线而无需提供参数,而由于没有满足任何路线,您当前会得到404?)

Does anyone know of or have an idea how I can retain the "strongly typed" nature of link generation with the new system or is it not possible anymore? 有谁知道或有一个想法如何保留新系统中链接生成的“强类型”性质,或者是不可能了?


Side Note 边注

An alternate solution might also be to generate a unique link for each category item (that way you could use link generation) but that means you have to generate a link for each category and send it in the response which would increase data sent to the browser (not by much I admit but every little helps) 另一种解决方案也可能是为每个类别项目生成一个唯一的链接(这样您就可以使用链接生成),但这意味着您必须为每个类别生成一个链接并在响应中发送它,这会增加发送到浏览器的数据(我承认不是很多,但有一点帮助)

Have you considered using taghelpers ? 您是否考虑过使用taghelpers?

https://docs.microsoft.com/en-us/aspnet/core/mvc/views/tag-helpers/built-in/anchor-tag-helper?view=aspnetcore-2.2 https://docs.microsoft.com/zh-cn/aspnet/core/mvc/views/tag-helpers/built-in/anchor-tag-helper?view=aspnetcore-2.2

Take a look at the asp-route-{value} section. 看一下asp-route- {value}部分。

https://docs.microsoft.com/en-us/aspnet/core/mvc/views/tag-helpers/built-in/anchor-tag-helper?view=aspnetcore-2.2#asp-route-value https://docs.microsoft.com/zh-cn/aspnet/core/mvc/views/tag-helpers/built-in/anchor-tag-helper?view=aspnetcore-2.2#asp-route-value

you could put a 'stub' value as route-value and replace it using your java-script. 您可以将“ stub”值作为route-value并使用Java脚本替换它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Asp.Net Core 端点路由 - Asp.Net Core Endpoint routing AuthorizeAttribute 不适用于 ASP.NET Core 3.1 中的端点路由 - AuthorizeAttribute not working with Endpoint Routing in ASP.NET Core 3.1 ASP.NET Core 3 MVC端点路由和按路由定位 - ASP.NET Core 3 MVC endpoint routing and localization by route ASP.NET Core 2.2中的端点路由不起作用 - Endpoint Routing in ASP.NET Core 2.2 is Not Working Asp.net Core 2.2 MVC 5路由可在View中使用更多子文件夹 - Asp.net core 2.2 mvc 5 routing for more subfolders in Views ASP.NET Web API (.NET Core 3.1) 中的端点路由问题 - Problem with endpoint routing in ASP.NET Web API (.NET Core 3.1) ASP.NET Core 3.0 端点路由不适用于默认路由 - ASP.NET Core 3.0 Endpoint Routing doesn't work for default route 在 ASP.Net Core 2.2 MVC 中使用 Endpoint-Routing 时如何正确覆盖 IUrlHelper? - How top correctly override the IUrlHelper while using Endpoint-Routing in ASP.Net Core 2.2 MVC? 在具有端点路由的 ASP.NET 核心中使用 CreatedAtRouteResult 生成带有分段的 URL - Generate a URL with segments using CreatedAtRouteResult in ASP.NET Core with endpoint routing ASP.NET Core 3.0 使用属性路由更改默认端点路由 - ASP.NET Core 3.0 change default endpoint route with attribute routing
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM