Jdenticon.AspNetCore Namespace

Classes for using Jdenticon from ASP.NET Core. This namespace contains classes for usage in Razor views, and an IActionResult for returning identicons from Mvc controllers.

Classes

Public classHtmlHelperExtensions
IHtmlHelper extension methods for Jdenticon.
Public classCode exampleIdenticonBuilderExtensions
Extends IApplicationBuilder with methods for enabling Jdenticon middleware, which will serve identicons to clients.
Public classIdenticonResult
Defines a result that will render and return an identicon.
Public classCode exampleIdenticonTagHelper
Tag helper used to display an identicon in <img> elements.
Public classUrlHelperExtensions
IUrlHelper extension methods for Jdenticon.

Examples

To use Jdenticon in a ASP.NET Core based application, install the Jdenticon.AspNetCore NuGet package.

NuGet Package Manager
PM> Install-Package Jdenticon.AspNetCore

After installing the NuGet package, enable Jdenticon in your applications by calling UseJdenticon(IApplicationBuilder) in Configure(IApplicationBuilder) in your Startup class. Put it right above UseStaticFiles(IApplicationBuilder).

Startup.cs
public class Startup
{
    /* ... */

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        /* ... */
        app.UseJdenticon();
        app.UseStaticFiles();
        app.UseMvc();
        /* ... */
    }
}

Make IdenticonTagHelper and the extensions for IHtmlHelper and IUrlHelper available to your views by adding the following code to _ViewImports.cshtml.

_ViewImports.cshtml
@using MyWebApplication
@namespace MyWebApplication.Pages
@addTagHelper*, Microsoft.AspNetCore.Mvc.TagHelpers
@using Jdenticon.AspNetCore
@addTagHelper "*, Jdenticon.AspNetCore"

Using tag helper

The identicon-value or identicon-hash attributes can be used to render identicons in Razor templates. The width and the height of the img tag is used to determine the dimensions of the generated icon. identicon-format is optional and defaults to PNG.

Using tag helper in a view
@{
    var userId = "TestIcon";
}
<div>
    An icon: <img identicon-value="userId" identicon-format="Png" alt="Identicon" width="60" height="60" />
</div>

<!-- is rendered as -->
<div>
    An icon: <img src="/jdenticon/5AMA8Xyneag78XyneQ--" alt="Identicon" width="60" height="60" />
</div>

Using @Html.Identicon

Jdenticon can be used in multiple ways in ASP.NET MVC. The first example shows how to use the @Html extension methods to render an IMG element on the page.

Using @Html.Identicon in a view
<div>
    An icon: @Html.Identicon("TestIcon", 60, alt: "Identicon")
</div>

<!-- is rendered as -->
<div>
    An icon: <img src="/identicons/5AMA8Xyneag78XyneQ--" width="60" height="60" alt="Identicon" />
</div>

Using @Url.Identicon

The following example shows how to use the @Url extension methods for generating identicon urls.

Using @Url.Identicon in a view
<div>
    An icon: <img src="@Url.Identicon("TestIcon", 60)" alt="Identicon" width="60" height="60" />
</div>

<!-- is rendered as -->
<div>
    An icon: <img src="/identicons/5AMA8Xyneag78XyneQ--" alt="Identicon" width="60" height="60" />
</div>

Returning an identicon from a MVC controller

It is also possible to return an identicon as a result from an ASP.NET MVC controller. Note that the actual icon data is returned by IdenticonResult. The other approaches only writes urls to the page output, which are later handled by the Jdenticon middleware.

Identicon as action result
public class IconController : Controller
{
    public ActionResult Icon(string value, int size)
    {
        return IdenticonResult.FromValue(value, size);
    }
}