Skip to content

Commit f157316

Browse files
author
Jeremy Hutchinson
committed
Remove characters should be %encoded from Slug
1 parent b4372eb commit f157316

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

Website/app_code/handlers/PostHandler.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Globalization;
34
using System.Linq;
5+
using System.Security.Policy;
46
using System.Text;
57
using System.Text.RegularExpressions;
68
using System.Web;
@@ -100,15 +102,28 @@ private byte[] ConvertToBytes(string base64)
100102

101103
public static string CreateSlug(string title)
102104
{
103-
title = title.ToLowerInvariant().Replace(" ", "-").Replace("#", "");
105+
title = title.ToLowerInvariant().Replace(" ", "-");
104106
title = RemoveDiacritics(title);
107+
title = RemoveReservedUrlCharacters(title);
105108

106109
if (Storage.GetAllPosts().Any(p => string.Equals(p.Slug, title, StringComparison.OrdinalIgnoreCase)))
107110
throw new HttpException(409, "Already in use");
108111

109112
return title.ToLowerInvariant();
110113
}
111114

115+
static string RemoveReservedUrlCharacters(string text)
116+
{
117+
var reservedCharacters = new List<string>() { "!", "#", "$", "&", "'", "(", ")", "*", ",", "/", ":", ";", "=", "?", "@", "[", "]", "\"", "%", ".", "<", ">", "\\", "^", "_", "'", "{", "}", "|", "~", "`", "+" };
118+
119+
foreach (var chr in reservedCharacters)
120+
{
121+
text = text.Replace(chr, "");
122+
}
123+
124+
return text;
125+
}
126+
112127
static string RemoveDiacritics(string text)
113128
{
114129
var normalizedString = text.Normalize(NormalizationForm.FormD);

0 commit comments

Comments
 (0)