From 111290133463ac1d765e32599e4f7cab94b73145 Mon Sep 17 00:00:00 2001 From: Roger Howell Date: Thu, 8 Feb 2024 17:48:34 +0000 Subject: [PATCH] Switch urls from CamelCase to kebab-case See also: https://www.gov.uk/guidance/content-design/url-standards-for-gov-uk https://learn.microsoft.com/en-us/aspnet/core/mvc/controllers/routing?view=aspnetcore-8.0#use-a-parameter-transformer-to-customize-token-replacement --- .../Program.cs | 10 +++++- .../SlugifyParameterTransformer.cs | 33 +++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 src/ServiceAssessmentService/ServiceAssessmentService.WebApp/SlugifyParameterTransformer.cs diff --git a/src/ServiceAssessmentService/ServiceAssessmentService.WebApp/Program.cs b/src/ServiceAssessmentService/ServiceAssessmentService.WebApp/Program.cs index 37f9ee93..a3c527ee 100644 --- a/src/ServiceAssessmentService/ServiceAssessmentService.WebApp/Program.cs +++ b/src/ServiceAssessmentService/ServiceAssessmentService.WebApp/Program.cs @@ -7,6 +7,8 @@ using ServiceAssessmentService.Application.Database; using ServiceAssessmentService.Application.UseCases; using ServiceAssessmentService.WebApp.Models; +using Microsoft.AspNetCore.Mvc.ApplicationModels; +using ServiceAssessmentService.WebApp; var builder = WebApplication.CreateBuilder(args); @@ -26,7 +28,13 @@ // By default, all incoming requests will be authorized according to the default policy. options.FallbackPolicy = options.DefaultPolicy; }); -builder.Services.AddRazorPages() + +builder.Services.AddRazorPages(options => + { + // Convert CamelCase to kebab-case for page routes + // See also: https://www.gov.uk/guidance/content-design/url-standards-for-gov-uk + options.Conventions.Add(new PageRouteTransformerConvention(new SlugifyParameterTransformer())); + }) .AddMicrosoftIdentityUI(); // // Used for local accounts diff --git a/src/ServiceAssessmentService/ServiceAssessmentService.WebApp/SlugifyParameterTransformer.cs b/src/ServiceAssessmentService/ServiceAssessmentService.WebApp/SlugifyParameterTransformer.cs new file mode 100644 index 00000000..3d765818 --- /dev/null +++ b/src/ServiceAssessmentService/ServiceAssessmentService.WebApp/SlugifyParameterTransformer.cs @@ -0,0 +1,33 @@ +using System.Text.RegularExpressions; + +namespace ServiceAssessmentService.WebApp; + +/// +/// This class is used to transform the URL parameters to lowercase and replace spaces with hyphens. +/// +/// See also the GDS guidance on URL, which requires lowercase and dash-separated words +/// https://www.gov.uk/guidance/content-design/url-standards-for-gov-uk +/// +/// For example: +/// +/// /AssessmentRequest/Details/1 +/// vs +/// /assessment-request/details/1 +/// +/// +/// Implementation is based on the following article: +/// https://learn.microsoft.com/en-us/aspnet/core/mvc/controllers/routing?view=aspnetcore-8.0#use-a-parameter-transformer-to-customize-token-replacement +/// +public class SlugifyParameterTransformer : IOutboundParameterTransformer +{ + public string? TransformOutbound(object? value) + { + if (value == null) { return null; } + + return Regex.Replace(value.ToString()!, + "([a-z])([A-Z])", + "$1-$2", + RegexOptions.CultureInvariant, + TimeSpan.FromMilliseconds(100)).ToLowerInvariant(); + } +}