Umbraco.Community.ModelAliases
1.0.0
dotnet add package Umbraco.Community.ModelAliases --version 1.0.0
NuGet\Install-Package Umbraco.Community.ModelAliases -Version 1.0.0
<PackageReference Include="Umbraco.Community.ModelAliases" Version="1.0.0" />
<PackageVersion Include="Umbraco.Community.ModelAliases" Version="1.0.0" />
<PackageReference Include="Umbraco.Community.ModelAliases" />
paket add Umbraco.Community.ModelAliases --version 1.0.0
#r "nuget: Umbraco.Community.ModelAliases, 1.0.0"
#:package Umbraco.Community.ModelAliases@1.0.0
#addin nuget:?package=Umbraco.Community.ModelAliases&version=1.0.0
#tool nuget:?package=Umbraco.Community.ModelAliases&version=1.0.0
Model Aliases
Property alias constants for your Umbraco Models Builder models, so ContentPage.Aliases.Title is "title"
and you never type an alias string by hand again.
What it does
Models Builder gives you a typed property for each field (Model.Title), but a lot of Umbraco code still asks for
the alias as a string: Value<T>("title"), HasValue("title"). Those strings are easy to mistype and nothing tells
you when they go stale. This package adds a nested Aliases class to every generated model, with one const string
per property:
// Before: a typo compiles, and quietly returns 0
var width = media.Value<int>("umbracoWidht");
// After: a typo is a build error
var width = media.Value<int>(Image.Aliases.UmbracoWidth);
The constants are written whenever Models Builder generates your models, into a separate <Model>.Aliases.gen.cs
file beside each stock <Model>.generated.cs. Your existing models are not edited.
Benefits
- No magic strings. A mistyped alias fails the build instead of returning an empty value at runtime.
- IntelliSense. Type
ContentPage.Aliases.and pick from the list. - Stays in step with your content types. Rename or remove a property and every place that used its constant stops compiling.
- Everything the model exposes, including properties from compositions and base types.
- Real constants, so they work in
switchcase labels and attribute arguments. - No setup. Nothing to configure and nothing in your existing models to change.
Installation
Add the package to the Umbraco website that runs Models Builder (any v17 release, from 17.0.0):
dotnet add package Umbraco.Community.ModelAliases
Usage
- Set
Umbraco:CMS:ModelsBuilder:ModelsModetoSourceCodeManualorSourceCodeAuto. - In the back office go to Settings, Models Builder, and press Generate models.
- Rebuild, then use the constants. They sit in your models' namespace:
@if (Model.HasValue(ContentPage.Aliases.Banner))
{
<p>@Model.Value<string>(ContentPage.Aliases.Title)</p>
}
The constant is named after the property, so alias pageTitle becomes Aliases.PageTitle.
Good to know
- Commit the
*.Aliases.gen.csfiles with your other generated models. A site that only consumes pre-built models (ModelsMode: Nothing, as on Umbraco Cloud) gets them that way. - Do not rename the files to end
.generated.cs: Umbraco deletes any of those it did not write itself. - A property named
Aliases, or two properties that end up with the same name, stops generation with a clear message. Change one of the aliases. - Requires Umbraco 17 (any release from 17.0.0), on .NET 10. It is not yet available for Umbraco 18.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- Umbraco.Cms.Core (>= 17.0.0 && < 18.0.0)
- Umbraco.Cms.Infrastructure (>= 17.0.0 && < 18.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.