SteinTech
SteinTech

Reputation: 4068

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?

Upvotes: 2

Views: 8272

Answers (3)

Chris Pratt
Chris Pratt

Reputation: 239460

Like the exception tells you, the main problem here is that you're simultaneously using both

  1. asp-page-handler and
  2. 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.

Upvotes: 3

Dwight
Dwight

Reputation: 730

Color

You are missing the last part of the asp-rout-color="docu"

Upvotes: 1

SteinTech
SteinTech

Reputation: 4068

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

Upvotes: 0

Related Questions