ASP .NET Core IDisplayMetadataProvider
Source: Dev.to
Introduction
Learn how to use a class that implements IDisplayMetadataProvider to transform property names such as FirstName to First Name.
Example for an HTML table
Example for a standard edit page
Using the provided class is not a replacement for the Display attribute; it is an alternative option.
Example class using the Display attribute
public partial class Person
{
public int PersonId { get; set; }
[Display(Name = "First Name")]
public string FirstName { get; set; }
[Display(Name = "Last Name")]
public string LastName { get; set; }
[Display(Name = "Email Address")]
public string EmailAddress { get; set; }
}
Example class using the custom provider
public partial class Person
{
public int PersonId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string EmailAddress { get; set; }
}
Using PascalCaseDisplayMetadataProvider
Setup in Program.cs
builder.Services.AddControllersWithViews(options =>
{
options.ModelMetadataDetailsProviders.Add(
new PascalCaseDisplayMetadataProvider(
[typeof(Person)],
includeDerivedTypes: false));
});
- Parameter 1: A collection of target types whose display metadata should be customized.
- Parameter 2: A boolean indicating whether derived types of the specified types should also be processed.
Alternate setup – dynamically load all classes in a folder
Helper method to retrieve class names from an assembly
static string[] GetClassNamesFromAssembly(Assembly assembly, string? @namespace = null) =>
assembly
.GetTypes()
.Where(t => t is { IsClass: true, IsAbstract: false })
.Where(t => @namespace == null || t.Namespace == @namespace)
.Select(t => t.Name)
.Distinct()
.OrderBy(n => n)
.ToArray();
Configure in the Main method
string ns = typeof(Program).Namespace!;
string[] classNames = GetClassNamesFromAssembly(
typeof(Program).Assembly,
$"{ns}.Models");
// Configures a custom display metadata provider to format PascalCase property names
// into a more readable format.
builder.Services.AddControllersWithViews(options =>
{
options.ModelMetadataDetailsProviders.Add(
new PascalCaseDisplayMetadataProvider(
[.. classNames
.Select(name => Type.GetType($"{ns}.Models.{name}"))
.Where(type => type is not null)
.Cast()],
includeDerivedTypes: false));
});
💡 Instead of copying PascalCaseDisplayMetadataProvider into each project, reference the AspCoreHelperLibrary package that contains the class.
Source code
The sample project demonstrates the PascalCaseDisplayMetadataProvider together with FluentValidation and CSS‑based required‑field indicators.
More to follow
Further articles on this topic will be published soon.

