Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to render a self closing tag using TagBuilder?

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?

like image 789
BrunoLM Avatar asked Jan 29 '11 23:01

BrunoLM


2 Answers

Have you tried:

canonical.ToString(TagRenderMode.SelfClosing); 
like image 71
Kirk Woll Avatar answered Sep 24 '22 10:09

Kirk Woll


You can also try.

TagBuilder  tagBuilder = new TagBuilder("link"); tagBuilder.TagRenderMode = TagRenderMode.SelfClosing; 
like image 20
maxspan Avatar answered Sep 23 '22 10:09

maxspan