Reputation: 4068
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
Reputation: 239460
Like the exception tells you, the main problem here is that you're simultaneously using both
asp-page-handler
andasp-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
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