How to render a self closing tag using TagBuilder?
Asked Answered
L

2

31

The following code:

var canonical = new TagBuilder("link");
canonical.MergeAttribute("rel", "canonical");
canonical.MergeAttribute("href", url);
return new MvcHtmlString(canonical.ToString());

Creates a tag:

<link href="http://local.domain.com/" rel="canonical"></link>

Is it possible to render

<link href="http://local.domain.com/" rel="canonical"/>

instead, using TagBuilder?

Legation answered 29/1, 2011 at 23:51 Comment(0)
M
81

Have you tried:

canonical.ToString(TagRenderMode.SelfClosing);
Matthews answered 29/1, 2011 at 23:55 Comment(2)
I actually didn't see there were that option in ToString. That worked, thank you.Legation
@rock, I'm sure Bruno knows how to accept. ;) And he has to wait a little bit before he's allowed to (since he just asked it).Matthews
E
3

You can also try.

TagBuilder  tagBuilder = new TagBuilder("link");
tagBuilder.TagRenderMode = TagRenderMode.SelfClosing;
Eraeradiate answered 1/8, 2016 at 23:1 Comment(1)
The TagRenderMode property seems to only be available on the Asp.Net core version of TagBuilder. learn.microsoft.com/en-us/dotnet/api/…Monique

© 2022 - 2024 — McMap. All rights reserved.