Tag Helpers
ImageSharp.Web includes Razor tag helpers so you can build image-processing URLs in strongly typed server-side markup instead of hand-concatenating query strings. The tag helpers also integrate with HMAC signing, which makes them the easiest way to generate safe image URLs in MVC and Razor Pages apps.
Enable the Tag Helpers
Add the ImageSharp namespaces and tag helper registration to _ViewImports.cshtml:
@using SixLabors.ImageSharp
@using SixLabors.ImageSharp.Processing
@using SixLabors.ImageSharp.Web
@addTagHelper *, SixLabors.ImageSharp.Web
That enables both ImageTagHelper and HmacTokenTagHelper.
Generate Command URLs with ImageTagHelper
ImageTagHelper targets <img> elements and converts imagesharp-* attributes into the corresponding query-string commands:
<img
src="images/hero.png"
imagesharp-width="400"
imagesharp-height="250"
imagesharp-rmode="ResizeMode.Crop"
imagesharp-format="Format.WebP"
imagesharp-quality="75"
alt="Hero image" />
That renders a src roughly like this:
images/hero.png?width=400&height=250&rmode=Crop&format=webp&quality=75
The built-in typed values mirror the processor APIs:
- Use
ResizeModeandAnchorPositionModefor resize modes and anchors. - Use
Colorforimagesharp-rcolorandimagesharp-bgcolor. - Use
Formatfor common output formats such asFormat.JpgandFormat.WebP. - Use
Resamplerfor common resamplers such asResampler.Lanczos3andResampler.NearestNeighbor.
The supported built-in attributes are:
imagesharp-width,imagesharp-height,imagesharp-rmode,imagesharp-ranchor,imagesharp-rxy,imagesharp-rcolor,imagesharp-rsampler,imagesharp-compand, andimagesharp-orientfor resize behavior.imagesharp-autoorientfor explicitly controlling EXIF-based rotation and flipping.imagesharp-formatfor output format selection.imagesharp-bgcolorfor flattening transparency.imagesharp-qualityfor JPEG and WebP quality.
If the <img> element does not already have literal width and height attributes, ImageTagHelper also writes them to the markup from the processing dimensions. That helps avoid layout shift for simple resize scenarios.
Because the middleware injects autoorient=true by default, you usually do not need imagesharp-autoorient="true" just to get correctly oriented output. The main reason to set the attribute explicitly is to opt out with imagesharp-autoorient="false" or to make the behavior explicit in markup.
Local URLs Versus External URLs
ImageTagHelper is intended for local application image URLs. It skips http, ftp, and data sources because ImageSharp.Web does not process those through the built-in local-path pipeline.
Automatic HMAC Generation
HmacTokenTagHelper runs on <img src="..."> and appends the hmac command automatically when ImageSharpMiddlewareOptions.HMACSecretKey is configured and the final src contains recognized commands.
That means both of these patterns work:
<img src="images/avatar.jpg?width=128&height=128&rmode=Crop" />
<img
src="images/avatar.jpg"
imagesharp-width="128"
imagesharp-height="128"
imagesharp-rmode="ResizeMode.Crop" />
In the first case, HmacTokenTagHelper signs your handwritten command URL. In the second case, ImageTagHelper generates the command URL and HmacTokenTagHelper signs it afterward.
Extending the Tag Helper
ImageTagHelper is unsealed. If you add custom processors and want matching Razor syntax, inherit from it and override AddProcessingCommands(...) to append your own commands before the final src is emitted.
Related Topics
Practical Guidance
- Use tag helpers when Razor should generate command URLs instead of hand-built query strings.
- Let the HMAC tag helper sign generated URLs when request signing is enabled.
- Keep custom tag helper commands aligned with custom processors.
- Inspect the emitted
srcduring troubleshooting so command order and token generation are visible.