Mvc: question: How to replace UrlResolutionTagHelper.

Created on 25 Jul 2016  路  6Comments  路  Source: aspnet/Mvc

How can I replace the default UrlResolutionTagHelper with my own custom one?

question

All 6 comments

Service way:

``` C#
public class CustomUrlResolutionTagHelper : UrlResolutionTagHelper
{
public CustomUrlResolutionTagHelper(
IUrlHelperFactory urlHelperFactory,
HtmlEncoder htmlEncoder)
: base(urlHelperFactory, htmlEncoder)
{
}

public override void Process(TagHelperContext context, TagHelperOutput output)
{
    base.Process(context, output);

    output.Attributes.SetAttribute("custom", "true");
}

}
...
services
.AddMvc()
.AddTagHelpersAsServices();

services.AddTransient();
...
```

Note: This does not require any additional @addTagHelper directives. It simply returns the CustomUrlResolutionTagHelper when the default one is requested.

Non-service way:

  • @removeTagHelper Microsoft.AspNetCore.Mvc.Razor.TagHelpers.UrlResolutionTagHelper, Microsoft.AspNetCore.Mvc.Razor - This will remove the default
  • @addTagHelper CustomUrlResolutionTagHelper, AssemblyWithMyCustomUrlResTagHelper - Add your own :smile:

Thank you @NTaylorMullen !! excellent!

I suspect Url.Content will NOT use the custom CustomUrlResolutionTagHelper logic ?

Nope.

It also doesn't change URL resolution in subclasses -- the <img>, <link> and <script> tag helpers.

We are closing this issue because no further action is planned for this issue. If you still have any issues or questions, please log a new issue with any additional details that you have.

Was this page helpful?
0 / 5 - 0 ratings