HtmlHelperExtensions | IHtmlHelper extension methods for Jdenticon.
| |
IdenticonBuilderExtensions |
Extends IApplicationBuilder with methods for enabling
Jdenticon middleware, which will serve identicons to clients.
| |
IdenticonResult |
Defines a result that will render and return an identicon.
| |
IdenticonTagHelper |
Tag helper used to display an identicon in <img> elements.
| |
UrlHelperExtensions | IUrlHelper extension methods for Jdenticon.
|
To use Jdenticon in a ASP.NET Core based application, install the Jdenticon.AspNetCore NuGet package.
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).
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.
@using MyWebApplication @namespace MyWebApplication.Pages @addTagHelper*, Microsoft.AspNetCore.Mvc.TagHelpers @using Jdenticon.AspNetCore @addTagHelper "*, Jdenticon.AspNetCore"
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.
@{ 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>
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.
<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>
The following example shows how to use the @Url extension methods for generating identicon urls.
<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>
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.
public class IconController : Controller { public ActionResult Icon(string value, int size) { return IdenticonResult.FromValue(value, size); } }