FriendToNetWebDevelopers.HtmlAttributeDictionary
1.2.5
See the version list below for details.
dotnet add package FriendToNetWebDevelopers.HtmlAttributeDictionary --version 1.2.5
NuGet\Install-Package FriendToNetWebDevelopers.HtmlAttributeDictionary -Version 1.2.5
<PackageReference Include="FriendToNetWebDevelopers.HtmlAttributeDictionary" Version="1.2.5" />
paket add FriendToNetWebDevelopers.HtmlAttributeDictionary --version 1.2.5
#r "nuget: FriendToNetWebDevelopers.HtmlAttributeDictionary, 1.2.5"
// Install FriendToNetWebDevelopers.HtmlAttributeDictionary as a Cake Addin #addin nuget:?package=FriendToNetWebDevelopers.HtmlAttributeDictionary&version=1.2.5 // Install FriendToNetWebDevelopers.HtmlAttributeDictionary as a Cake Tool #tool nuget:?package=FriendToNetWebDevelopers.HtmlAttributeDictionary&version=1.2.5
HTML Attribute Dictionary
Summary
A dictionary which extends Dictionary<string,string> and provides html attribute and value safety features.
Some of these can change regularly. Please see the CHANGELOG.md file for more details.
Installation
Install-Package FriendToNetWebDevelopers.HtmlAttributeDictionary
Usage
How?
Add the following to your _ViewImports.cshtml
@using FriendToNetWebDevelopers.HtmlAttributeDictionary
@addTagHelper *, FriendToNetWebDevelopers.HtmlAttributeDictionary
This is the simplest use case I could come up with.
@{ // Please note the multiple classes being brought in
// Along with your id ↓ ↓
// ↓ ↓ ↓
var dictionary = HtmlAttributeDictionaryFactory.WithId("foo", "some-hero bar");
if(needsScrim)
{
dictionary.AddClassToClassAttribute("hero-scrim");
}
}
<section>
<div html-attributes="dictionary">
</div>
</section>
-or just with classes-
@{
var dictionary = HtmlAttributeDictionaryFactory.WithClasses("some-hero bar");
if(needsScrim)
{
dictionary.AddClassToClassAttribute("hero-scrim");
}
}
<section>
<div @Html.Raw(dictionary)>
</div>
</section>
But can be initialized in the following ways as well.
//Provides an empty dictionary
var dictionary = HtmlAttributeDictionaryFactory.Get();
//ALSO provides an empty dictionary
dictionary = new HtmlAttributeDictionary();
//Follows the safety procedures to attempt to bring in the attributes from a different type of dictionary
var someOtherDictionary = new Dictionary<string, string>();
dictionary = new HtmlAttributeDictionary(someOtherDictionary);
//Follows the safety procedures to attempt to bring in the attributes from a list of key-value-pairs
var list = new KeyValuePair<string,string>();
dictionary = new HtmlAttributeDictionary(list);
While I like using the factory when producing the attribute dictionary inline, that's just because of the shortcuts it provides. This can be produced entirely dynamically and still work.
Why?
This keeps things simple and safe. It even allows for data to come in from an unsafe source and be safe to display.
It even prevents invalid attributes from being added.
What if there's a problem
Please feel free to report any issues here on GitHub.
Include as many details as possible and one of us will check it out.
Thanks
A big thank you to Joshua Hess for his initial review and contribution of the Tag Helper feature.
I would also like to extend my gratitude to my employer, Heartland Business Systems, for putting me in a position to help others by providing this helper library to others free of charge. They've supported me in creating this work and share it openly with .NET Web Developers everywhere.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net8.0
- Microsoft.AspNetCore.Razor (>= 2.2.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Added ability to use on openeing tag only