Reputation: 4068
I have a custom TagHelper I need to pass a Guid to, but I get errors when I pass the value. How can I pass in a Guid?
TagHelper:
[HtmlTargetElement("assets:signin", Attributes = "profile", TagStructure = TagStructure.NormalOrSelfClosing)]
public sealed class GoToSignInTagHelper(IWebHostEnvironment environment, IHtmlHelper html, ITagHelperRepo helperRepo, IUtilityHelper helpUtil) : TagHelperCustom(environment, helperRepo, html)
{
public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
{
await base.PreProcessAsync(context, output);
ITagBuilderCustom link = new TagBuilderCustom("a");
link.AddAttribute("href", $"{CoreStatics.AssetsURL}/SignIn?uid={this.Profile}");
AddContent(link);
await base.ProcessAsync();
}
[HtmlAttributeName("profile")]
public Guid Profile { get; set; }
}
Cshtml:
<assets:signin profile="5073c3f0-c4ac-4be8-ae7f-9b23ae5e640e" />
The errors I get when I build the project:
Upvotes: 1
Views: 56
Reputation: 51260
Reproduce from my end, you cannot directly provide the Guid
(string) to the profile
attribute as your current way which results in the compilation error.
Provide the Guid
value as:
<assets:signin profile='@Guid.Parse("5073c3f0-c4ac-4be8-ae7f-9b23ae5e640e")' />
or
@{
Guid guid = Guid.Parse("5073c3f0-c4ac-4be8-ae7f-9b23ae5e640e");
}
<assets:signin profile="@guid" />
Another approach is that the profile attribute to accept the string value, then you should have another property/variable to convert the string into Guid
.
[HtmlTargetElement("assets:signin", Attributes = "profile", TagStructure = TagStructure.NormalOrSelfClosing)]
public sealed class GoToSignInTagHelper(IWebHostEnvironment environment, IHtmlHelper html, ITagHelperRepo helperRepo, IUtilityHelper helpUtil) : TagHelperCustom(environment, helperRepo, html)
{
[HtmlAttributeName("profile")]
public string ProfileString { get; set; }
public Guid Profile
{
get { return Guid.Parse(ProfileString); }
}
...
}
So you can provide the value as below:
<assets:signin profile="5073c3f0-c4ac-4be8-ae7f-9b23ae5e640e" />
Upvotes: 1