简体   繁体   中英

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.

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 . The following attributes are mutually exclusive: 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.

Any advice?

Like the exception tells you, the main problem here is that you're simultaneously using both asp-page-handler and asp-route . The first is for generating a URL to a Razor Page, while the latter is for generating a URL to a named route. 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"

I ended up with usin the IUrlHelper in a custom tag helper instead of using the anchor helper.

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

    return factory.GetUrlHelper(actionContext);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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