Helper method to encode query string properly
When using Url.ContentUrl() in Optimizely 12, encodes spaces as + in the query string. If you want to encode the spaces as %20, use the below helper method.
public static string GetEncodedUrl(string href)
{
var decodedHref = System.Web.HttpUtility.UrlDecode(href);
int questionMarkIndex = decodedHref.IndexOf('?');
string path = questionMarkIndex >= 0 ? decodedHref.Substring(0, questionMarkIndex) : decodedHref;
var relativeUrl = UrlResolver.GetUrl(path);
string query = questionMarkIndex >= 0 ? decodedHref.Substring(questionMarkIndex + 1).Replace("&", "&") : string.Empty;
var encodedQuery = !string.IsNullOrEmpty(query) ? $"?{Uri.EscapeUriString(query)}" : query;
return $"{relativeUrl}{encodedQuery}";
}
Comments