Remove Tilde from URLs for MVC
Currently, when you create an HTML field in Kentico and then render it into a view on an MVC site, it appends a tilde (~) to the start of any relative URL path.
This causes links to break and according to Kentico Support this is the expected behavior.
It seems like there should be an option to remove the tilde.
This is in reference to Support Ticket #0094000107.
Eric Rovtar commented
For anyone wondering how to work around this current issue, here is the excellent response they gave me:
I've discussed this issue with our developers and regrettably I have to say that this behavior is caused by the design at the moment, there is no setting governing relative path in links.
But you can customize this behavior:
1. You can either use regex to replace ‘~’ … that is one line of code:
var output = Regex.Replace(html, @"[(,"",']~\/", m => m.Groups.Value.Replace("~/", applicationPath));
2. Or you can use our method, which utilizes StringBuilder and is faster:
/// Resolves all urls starting with '~' character to relative paths.
/// <param name="html">Input html content.</param>
/// <returns>Resolved urls to relative paths.</returns>
public static MvcHtmlString ResolveUrls(string html)
var urlHelper = new UrlHelper(instance.Target.ViewContext.RequestContext);
var applicationPath = urlHelper.Content("~/").TrimEnd('/');
var pathIndex = html.IndexOfCSafe("~/");
if (pathIndex >= 1)
var sb = new StringBuilder((int)(html.Length * 1.1));
var lastIndex = 0;
while (pathIndex >= 1)
if ((html[pathIndex - 1] == '(') || (html[pathIndex - 1] == '"') || (html[pathIndex - 1] == '\''))
// Add previous content
if (lastIndex < pathIndex)
sb.Append(html, lastIndex, pathIndex - lastIndex);
// Add application path and move to the next location
lastIndex = pathIndex + 1;
pathIndex = html.IndexOfCSafe("~/", pathIndex + 2);
// Add the rest of the content
if (lastIndex < html.Length)
sb.Append(html, lastIndex, html.Length - lastIndex);
html = sb.ToString();
return new MvcHtmlString(html);
Please let me know if there's anything else I can do for you.