简体   繁体   English

尝试在锚标记助手中使用自定义路由时出错

[英]getting error when trying to use custom route in anchor tag helper

I've created a route with template " /documentation/{category?}/{feature?} " and named it docu , but I get an error when I try to use it in the anchor tag helper.我已经使用模板“ /documentation/{category?}/{feature?} ”创建了一个路由,并将其命名为docu ,但是当我尝试在锚标记帮助器中使用它时出现错误。

Link:关联:

<a asp-route="docu" asp-route-category="layout" asp-route-feature="colors" asp-page-handler="Feature" class="link">Color</a>

Error:错误:

InvalidOperationException: Cannot determine the 'href' attribute for . InvalidOperationException: 无法确定 的“href”属性。 The following attributes are mutually exclusive: asp-route asp-controller, asp-action asp-page, asp-page-handler以下属性是互斥的:asp-route asp-controller、asp-action asp-page、asp-page-handler

It works if I use @Url.RouteUrl() in the cshtml file, but I don't know if I get access to it in a tag helper.如果我在 cshtml 文件中使用@Url.RouteUrl() ,它会起作用,但我不知道我是否可以在标签助手中访问它。

Any advice?有什么建议吗?

Like the exception tells you, the main problem here is that you're simultaneously using both asp-page-handler and asp-route .就像异常告诉您的那样,这里的主要问题是您同时使用asp-page-handlerasp-route The first is for generating a URL to a Razor Page, while the latter is for generating a URL to a named route.第一个用于生成 Razor 页面的 URL,而后者用于生成命名路由的 URL。 The two are mutually exclusive, so you simply need to pick one and remove the other.两者是相互排斥的,因此您只需选择一个并删除另一个。

Color颜色

You are missing the last part of the asp-rout-color="docu"你错过了 asp-rout-color="docu" 的最后一部分

I ended up with usin the IUrlHelper in a custom tag helper instead of using the anchor helper.我最终在自定义标签助手中使用了 IUrlHelper,而不是使用锚助手。

services.AddScoped<IUrlHelper>(x =>
{
    var actionContext = x.GetRequiredService<IActionContextAccessor>().ActionContext;
    var factory = x.GetRequiredService<IUrlHelperFactory>();

    return factory.GetUrlHelper(actionContext);
});

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM