Very nice article, I currently use a custom html h...
# news
h
Very nice article, I currently use a custom html helper which allows me to render css/scrippt blocks in partials and have it rendered in the right places (styles in head etc) I may look at the taghelper approach though after reading your article.
d
I especially love how natural the html looks using the tag helper. That's important to me because the frontend developers that I work with don't usually know how to use things like html helpers.
h
You mght want to change
output.Attributes.Remove(existingAttribute);
into
Copy code
if (existingAttribute != null)
            {
                output.Attributes.Remove(existingAttribute);
            }
If the element has a style and no existing class it errors 😄
d
oof, good observation! I'll fix that right away!
Actually, looking at the docs, it's also fine without the null check:
h
odd, it errored for me if it was null
d
I guess a null check can't hurt and shows more intent anyway, I'll add it in the post
It's strange that visual studio doesn't give a warning about nullability here in my solution
h
@D_Inventor I also noticed that this breaks inline styles in the backoffice for blocks 😄
I added this to the top of the process method
Copy code
if (_httpContextAccessor.HttpContext.Request.IsBackOfficeRequest())
            {
                output.Attributes.Add(new TagHelperAttribute("style", new HtmlString(Style)));
                return;
            }
d
Ah, good to know, I'll add that to my example. You're awesome for trying it out and giving me valuable feedback like this 😊
h
You're welcome, it's a great little taghelper.
7 Views