First of all, thanks to Tirius, Romias and eth0 for their suggestions.
I decided that I did not want to use Id for the category, and I did not want to create a route handler, since this does not solve the root problem.
Instead, I create a validation attribute called "UsedAsUrl" and apply it to my .Name category in my domain model. This has the advantages of built-in validation (good for the end user) and good reuse from a developer's point of view.
So, my model model now looks like this (note the [UsedAsUrl] attribute):
public class Category { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int Id { get; set; } [Required] [UsedAsUrl] [MaxLength(50, ErrorMessage = "One word or groups of words please")] public string Name { get; set; } public virtual List<Post> Posts { get; set; } }
And the attribute I created looks like this:
using System; using System.ComponentModel.DataAnnotations; using System.Text.RegularExpressions; namespace CommonWebsiteUtilities.Attributes { [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = true)] public class UsedAsUrlAttribute : ValidationAttribute { public UsedAsUrlAttribute() : base("Must not contain a slash with spaces either side") {} public override bool IsValid(object value) { var input = value.ToString(); var result = true; if (input.Contains("/")) { input = Regex.Replace(input, @"\s+", " "); if (input.Contains(" /")) result = false; if (input.Contains("/ ")) result = false; if (input.Contains(" / ")) result = false; } return result; } } }
Now when I go to add a category:

I get this answer automatically:

JS is not working yet, but my controller can pick up the state of the model, and this is a response from it, so the attribute works correctly.
This basically checks for a slash with spaces on either side. Please note that this is not an exhaustive mechanism for checking URLs, but it will be used for my current project. If anyone has any improvements, let me know and I will correct this answer.
I am not good at RegExp, so I didn’t get from RegularExpressionAttribute, and also didn’t want me to have to output an error message from my model. This attribute should really be based on the fact that when using categories as URLs, more rules appear.