diff --git a/src/Controllers/BlogController.cs b/src/Controllers/BlogController.cs index 6e49f017..4286af2e 100644 --- a/src/Controllers/BlogController.cs +++ b/src/Controllers/BlogController.cs @@ -222,6 +222,13 @@ public async Task UpdatePost(Post post) } var existing = await this.blog.GetPostById(post.ID).ConfigureAwait(false) ?? post; + var existingPostWithSameSlug = await this.blog.GetPostBySlug(existing.Slug).ConfigureAwait(true); + + if (existingPostWithSameSlug != null && existingPostWithSameSlug.ID != post.ID) + { + + existing.Slug = Models.Post.CreateSlug(post.Title + DateTime.UtcNow.ToString("yyyyMMddHHmm"),50); + } string categories = this.Request.Form[Constants.categories]; string tags = this.Request.Form[Constants.tags]; diff --git a/src/Models/Post.cs b/src/Models/Post.cs index 00409533..94de8069 100644 --- a/src/Models/Post.cs +++ b/src/Models/Post.cs @@ -38,13 +38,20 @@ public class Post public string Title { get; set; } = string.Empty; [SuppressMessage("Globalization", "CA1308:Normalize strings to uppercase", Justification = "The slug should be lower case.")] - public static string CreateSlug(string title) + public static string CreateSlug(string title, int maxLength = 50) { + title = title?.ToLowerInvariant().Replace( Constants.Space, Constants.Dash, StringComparison.OrdinalIgnoreCase) ?? string.Empty; title = RemoveDiacritics(title); title = RemoveReservedUrlCharacters(title); + // control of character limitations + if (title.Length > maxLength) + { + title = title.Substring(0, maxLength); + } + return title.ToLowerInvariant(); } diff --git a/src/Views/Blog/Edit.cshtml b/src/Views/Blog/Edit.cshtml index 884a869e..341b9dc0 100644 --- a/src/Views/Blog/Edit.cshtml +++ b/src/Views/Blog/Edit.cshtml @@ -21,7 +21,7 @@
- + The part of the URL that identifies this blog post