Extension to Razor HtmlHelper

How to export a C# enum:

public enum AssetStatusEnum
{
    Free,
    Reserved,
    Inactive,
    UnderMaintenance
}

to

    var assetStatusEnum = {"Free":0,"Reserved":1,"Inactive":2,"UnderMaintenance":3}

In implementing an extension method:

public static class HtmlEnumExtensions
{
    public static MvcHtmlString EnumToString<T>(this HtmlHelper helper)
    {
        var values = Enum.GetValues(typeof(T)).Cast<int>();
        var enumDictionary = values.ToDictionary(value => Enum.GetName(typeof(T), value));
 
        return new MvcHtmlString(JsonConvert.SerializeObject(enumDictionary));
    }
}

and using it like this:

    var assetStatusEnum = @(Html.EnumToString())

 

Converting C# enums to JavaScript | Gunnar Peipman – Programming Blog

Advertisements

sritest.io – SRI Hash Website Scanner

Subresource Integrity (SRI) is a way to secure external dependencies of web pages. From Mozilla’s technical page:

You use the Subresource Integrity feature by specifying a base64-encoded cryptographic hash of a resource (file) you’re telling the browser to fetch, in the value of the integrity attribute of any <script> or <link> element.

An integrity value begins with at least one string, with each string including a prefix indicating a particular hash algorithm (currently the allowed prefixes are sha256sha384, and sha512), followed by a dash, and ending with the actual base64-encoded hash.

An integrity value may contain multiple hashes separated by whitespace. A resource will be loaded if it matches one of those hashes.

Supported in Chrome 45+, Firefox 43+, recently (2017-05-09) in Safari but “UNDER CONSIDERATION” in Edge since August 06, 2014.

More on this subject here.

Online scanner: sritest.io – SRI Hash Website Scanner