@if (!String.IsNullOrWhiteSpace(Model.Url))
{
<a href="@Model.Url"><img src="image.jpg" /></a>
}
else
{
<img src="image.jpg" />
}
<a asp-conditional href="@Model.Url"><img src="image.jpg" /></a>
namespace SecretOrange | |
{ | |
[HtmlTargetElement("a", Attributes = "asp-conditional")] | |
public class ConditionalAnchorTagHelper : TagHelper | |
{ | |
public override async void Process(TagHelperContext context, TagHelperOutput output) | |
{ | |
var href = context.AllAttributes["href"]?.Value.ToString(); | |
if (String.IsNullOrWhiteSpace(href)) | |
{ | |
output.SuppressOutput(); | |
var childContent = await output.GetChildContentAsync(); | |
output.Content.SetHtmlContent(childContent); | |
} | |
else | |
{ | |
output.Attributes.Remove(output.Attributes["asp-conditional"]); | |
} | |
} | |
} | |
} |
hmmm useful Tag helper to prevent if..else... in Razor.
thanx