Episode 8

Umbraco 13 Tutorial - Episode 8 - C# and Dotnet Features

Code

MediaWithCropsExtensions.cs

namespace Freelancer.Extensions;

public static class MediaWithCropsExtensions
{
    public static string GetAltText(this MediaWithCrops mediaItem, string altTextAlias = "altText")
    {
        var altText = mediaItem.Value<string>(altTextAlias);

        return string.IsNullOrWhiteSpace(altText) ? string.Empty : altText;
    }
}

MediaWithCropsExtensions.cs

PublishedContentExtensions.cs

namespace Freelancer.Extensions;

public static class PublishedContentExtensions
{
    public static HomePage? GetHomePage(this IPublishedContent publishedContent)
    {
        return publishedContent.AncestorOrSelf<HomePage>();
    }

    public static SiteSettings? GetSiteSettings(this IPublishedContent publishedContent)
    {
        var homePage = GetHomePage(publishedContent);
        return homePage?.FirstChild<SiteSettings>();
    }

}

PublishedContentExtensions.cs

Freelancer.csproj

<Target Name="PreBuild" BeforeTargets="PreBuildEvent">
    <Exec Command="dotnet format --severity warn --verbosity diagnostic" />
</Target>

Freelancer.csproj

GlobalUsings.cs

global using Umbraco.Cms.Core.Models;
global using Umbraco.Cms.Core.Models.PublishedContent;
global using Umbraco.Cms.Web.Common.PublishedModels;

GlobalUsings.cs

TestClass.cs

using Umbraco.Cms.Core.Services;

namespace Freelancer.Testing;

public class TestClass(IMediaService mediaService)
{
    public void DoSomething()
    {
        var thing = mediaService.GetById(1);
    }
}

TestClass.cs

ContentPage.cshtml

@inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage<ContentModels.ContentPage>

@{
    Layout = "Main.cshtml";
}

@await Html.GetBlockGridHtmlAsync(Model, "headerContent")
@await Html.GetBlockGridHtmlAsync(Model, "mainContent")

ContentPage.cshtml

_ViewImports.cshtml

@using Freelancer.Extensions
@using Umbraco.Cms.Core.Services
@using Umbraco.Extensions
@using Umbraco.Cms.Web.Common.PublishedModels;
@using ContentModels = Umbraco.Cms.Web.Common.PublishedModels;
@using Umbraco.Cms.Web.Common.Views
@using Umbraco.Cms.Core.Models.Blocks
@using Umbraco.Cms.Core.Models.PublishedContent
@using Microsoft.AspNetCore.Html
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@addTagHelper *, Smidge
@addTagHelper *, Our.Umbraco.TagHelpers
@inject Smidge.SmidgeHelper SmidgeHelper
@using Slimsy.Enums;
@addTagHelper *, Slimsy
@inject Slimsy.Services.SlimsyService SlimsyService
@using Slimsy.Extensions

_ViewImports.cshtml