Skip to content

Commit

Permalink
Merge pull request #180 from DFE-Digital/kebab-case-urls
Browse files Browse the repository at this point in the history
Switch URLs from CamelCase to kebab-case
  • Loading branch information
RogerHowellDfE authored Feb 8, 2024
2 parents 2be7e02 + 1112901 commit 21268f7
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System.Text.RegularExpressions;

namespace ServiceAssessmentService.WebApp;

/// <summary>
/// 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:
/// <code>
/// /AssessmentRequest/Details/1
/// vs
/// /assessment-request/details/1
/// </code>
///
/// 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
/// </summary>
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();
}
}

0 comments on commit 21268f7

Please sign in to comment.