Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to pass a Guid to a property on a Tag Helper in ASP.NET Core

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<assets:signin profile="5073c3f0-c4ac-4be8-ae7f-9b23ae5e640e" />

The errors I get when I build the project:
enter image description here

>Solution :

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" />
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading