Reputation: 3521
Hello this is my tag helper
[HtmlTargetElement("card")]
public class CardTagHelper : TagHelper
{
public string Title { get; set; }
public string Icon { get; set; }
public string Url { get; set; }
public override void Process(TagHelperContext context, TagHelperOutput output)
{
output.TagName = "CardTagHelper";
output.TagMode = TagMode.StartTagAndEndTag;
var preContent = new StringBuilder();
preContent.AppendFormat($@"
<div class='card custom-card'>
<div class='card-header'>
<div class='card-title'>");
if (Url != null)
{
preContent.AppendFormat($@"
<a asp-for='{Url}'>
<div class='float-left return'>
<i class='fas fa-arrow-alt-circle-left fa-lg'></i>
</div>
</a>");
}
preContent.AppendFormat($@"<i class='{Icon}'></i>
{Title}
</div>
</div>
<div class='card-body'>");
var postContent = new StringBuilder();
postContent.AppendFormat($@"
</div>
<div class='card-footer'>
</div>
</div>");
output.PreContent.SetHtmlContent(preContent.ToString());
output.PostContent.SetHtmlContent(postContent.ToString());
}
}
}
the output is a bootstrap 4 card
<card title="myTitle" icon="myIcon" url="redirectUrl">
// Content here
</card>
my problem is that i would like to use asp-page instead of href on the conditional rendered string for the anchor but when i added it, it had no effect or any clickable.
EDIT
These are my viewimports, as suggested i tried include my custom helper before the Asp.Net Core
@using MyProject
@namespace MyProject.Pages
@addTagHelper *, MyProject
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
Upvotes: 2
Views: 1044
Reputation: 1163
However, I didn't find any article where it is explained how we can use Razor Tag helpers inside our custom tag helpers. but for my solution, i had to use actual 'href' tag instead of 'asp-for' razor tag helper like below.
public string Url { get; set; }
public override void Process(TagHelperContext context, TagHelperOutput output)
{
var content = $@"<a class='nav-link text-dark' href='/{Url}'>{Title}</a>";
output.TagName = "li";
output.Content.SetHtmlContent(content);
output.Attributes.Add("class", "nav-item");
}
Razor tag helper did behave the same it is doing for you, @Jackal. HTML generated like below -
<li class="nav-item"><a class="nav-link text-dark" href="/Index">Home</a></li>
<li class="nav-item"><a class="nav-link text-dark" href="/Restaurants/List">Restaurants</a></li>
Hope, it helps. Thanks
Upvotes: 1