XperienceCommunity.PageLinkTagHelpers 1.0.0-prerelease-1-1

This is a prerelease version of XperienceCommunity.PageLinkTagHelpers.
There is a newer version of this package available.
See the version list below for details.
dotnet add package XperienceCommunity.PageLinkTagHelpers --version 1.0.0-prerelease-1-1
NuGet\Install-Package XperienceCommunity.PageLinkTagHelpers -Version 1.0.0-prerelease-1-1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="XperienceCommunity.PageLinkTagHelpers" Version="1.0.0-prerelease-1-1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add XperienceCommunity.PageLinkTagHelpers --version 1.0.0-prerelease-1-1
#r "nuget: XperienceCommunity.PageLinkTagHelpers, 1.0.0-prerelease-1-1"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install XperienceCommunity.PageLinkTagHelpers as a Cake Addin
#addin nuget:?package=XperienceCommunity.PageLinkTagHelpers&version=1.0.0-prerelease-1-1&prerelease

// Install XperienceCommunity.PageLinkTagHelpers as a Cake Tool
#tool nuget:?package=XperienceCommunity.PageLinkTagHelpers&version=1.0.0-prerelease-1-1&prerelease

GitHub Actions CI: Build

Publish Packages to NuGet

NuGet Package

Kentico Xperience 13.0 ASP.NET Core Tag Helpers that generates links to pages from NodeGUID values

Dependencies

This package is compatible with ASP.NET Core 3.1+ applications or libraries integrated with Kentico Xperience 13.0.

How to Use?

  1. Install the NuGet package in your ASP.NET Core project (or class library)

    dotnet add package XperienceCommunity.PageLinkTagHelpers
    
  2. Add the correct @addTagHelper directive to your _ViewImports.cshtml file:

    @addTagHelper *, XperienceCommunity.PageLinkTagHelpers

  3. Create an implementation of ILinkablePage:

    public class LinkablePage : ILinkablePage
    {
        public static LinkablePage Home { get; } = new LinkablePage(new Guid("..."));
        public static LinkablePage Store { get; } = new LinkablePage(new Guid("..."));
        public static LinkablePage ContactUs { get; } = new LinkablePage(new Guid("..."));
        public static LinkablePage TermsOfUse { get; } = new LinkablePage(new Guid("..."));
    
        public Guid NodeGUID { get; }
    
        protected LinkablePage(Guid nodeGUID) => NodeGUID = nodeGUID;
    
        public static IReadOnlyList<LinkablePage> All { get; } = new List<LinkablePage>
        {
            Home,
            Store,
            ContactUs,
            TermsOfUse
        };
    }
    
  4. Register the library with ASP.NET Core DI:

    public void ConfigureServices(IServiceCollection services)
    {
        // Use default implementations
        services.AddXperienceCommunityPageLinks();
    
        // or use a custom implementation
        services.AddXperienceCommunityPageLinks<MyCustomLinkablePageLinkRetriever>();
    }
    
  5. (optional) Add your LinkablePage class's namespace to your _ViewImports.cshtml file.

  6. Use the xp-page-link tag helper on an <a> element in a Razor View:

    <a href="" xp-page-link="LinkablePage.Home">
      <img
        src="/getmedia/10d5e094-d9aa-4edf-940d-098ca69b5f77/logo.png"
        alt="..."
      />
    </a>
    
  7. (recommended) Create a global event handler to protect the Pages referenced by your ILinkablePage implementation:

    using System;
    using System.Linq;
    using CMS;
    using CMS.Core;
    using CMS.DataEngine;
    using CMS.DocumentEngine;
    
    [assembly: RegisterModule(typeof(LinkablePageProtectionModule))]
    
    namespace Sandbox
    {
        /// <summary>
        /// Protects <see cref="LinkablePage"/> instances that represent Pages in the content tree with hard coded <see cref="TreeNode.NodeGUID"/> values.
        /// </summary>
        public class LinkablePageProtectionModule : Module
        {
            public LinkablePageProtectionModule() : base(nameof(LinkablePageProtectionModule)) { }
    
            protected override void OnInit()
            {
                base.OnInit();
    
                DocumentEvents.Delete.Before += Delete_Before;
            }
    
            private void Delete_Before(object sender, DocumentEventArgs e)
            {
                if (LinkablePage.All.Any(p => p.NodeGuid == e.Node.NodeGUID))
                {
                    e.Cancel();
    
                    var log = Service.Resolve<IEventLogService>();
    
                    log.LogError(
                        nameof(LinkablePageProtectionModule),
                        "DELETE_PAGE",
                        $"Cannot delete Linkable Page [{e.Node.NodeAliasPath}], as it might be in use. Please first remove the Linkable Page in the application code and re-deploy the application.");
                }
            }
        }
    }
    

Usage

<a
  href=""
  xp-page-link="LinkablePage.ContactUs"
  xp-page-link-text="Contact us for help!"
  xp-page-link-query-params='new NameValueCollection { { "a": "b" } }'
></a>

This will generate the following HTML:

<a href="/contact-us?a=b">Contact us for help!</a>

References

.NET

Kentico Xperience

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp3.1 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
3.0.0 1,407 10/10/2022
3.0.0-prerelease-8-2 145 10/10/2022
2.0.0 446 8/20/2022
2.0.0-prerelease-7-1 110 8/20/2022
1.0.0 574 5/7/2022
1.0.0-prerelease-6-1 142 5/7/2022
1.0.0-prerelease-5-1 123 5/7/2022
1.0.0-prerelease-4-1 152 5/7/2022
1.0.0-prerelease-3-1 143 5/7/2022
1.0.0-prerelease-1-1 140 5/4/2022