Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First off, thanks to Tyrsius, Romias and eth0 for their suggestions.</p> <p>I decided that I didn't want to use the Id for a category and I didn't want to create a route handler as this isn't really solving the root issue.</p> <p>Instead I create a validation attribute called <strong>"UsedAsUrl"</strong>, and applied this to my Category.Name in my domain model. This has the benefits of inbuilt validation (good for the end user) and good re-usability from a developer perspective.</p> <p>So my category model now looks like this (note the [UsedAsUrl] attribute):</p> <pre><code>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&lt;Post&gt; Posts { get; set; } } </code></pre> <p>And the attribute I created looks like this:</p> <pre><code>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; } } } </code></pre> <p>Now when I go to add a category:</p> <p><img src="https://i.stack.imgur.com/5mIvX.png" alt="adding a category that I know will fail validation"></p> <p>I get this response automatically:</p> <p><img src="https://i.stack.imgur.com/NQVqR.png" alt="Failed response with validation"></p> <p>JS doesn't yet work, but my controller can pick up on the model state and this is the response from it, so the attribute is working correctly.</p> <p>This basically checks for a slash with spaces on either / any side. Please note that this is not an exhaustive URL validator, but it will do for my current project. If anyone has any improvements, please let me know and I'll amend this answer. </p> <p>I'm not good with RegExp, so didn't derive from RegularExpressionAttribute, and also I didn't want to have to supply the error message from inside my model. This Attribute should really be built upon as more rules appear when using categories as URL parts.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload