A critical vulnerability was discovered in React Server Components (Next.js). Our systems remain protected but we advise to update packages to newest version. Learn More

sunylcumar
May 13, 2025
  583
(5 votes)

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}";
 }
May 13, 2025

Comments

El Magnifico
El Magnifico May 18, 2025 02:14 PM

works well

Manoj Kumawat
Manoj Kumawat May 20, 2025 07:37 AM

Hi, thank you for sharing your insights and contributing to the community.

That said, I wanted to share some constructive feedback. Some of the posts could benefit from a bit more context around the problem being addressed. Providing additional background can help readers better understand the issue and how your solution applies.

Additionally, I've noticed that many posts seem to have the same reviewer, which raises some concerns about objectivity. It's important that content on World Optimizely not only be informative but also genuinely helpful and independently reviewed.

I hope you take this feedback in the spirit it's intended to support continued improvement. Wishing you all the best, and thank you again for contributing.

sunylcumar
sunylcumar May 22, 2025 03:48 PM

Hi Manoj,

First of all I thank you for your suggestion about providing the context of the problem and I agree with you. I will try to explain the problem and solutions in my future posts.

FYI, The other reviewer is my friend for whom I post the solutions here instead of just helping him

Thanks

Sunil

Please login to comment.
Latest blogs
Building simple Opal tools for product search and content creation

Optimizely Opal tools make it easy for AI agents to call your APIs – in this post we’ll build a small ASP.NET host that exposes two of them: one fo...

Pär Wissmark | Dec 13, 2025 |

CMS Audiences - check all usage

Sometimes you want to check if an Audience from your CMS (former Visitor Group) has been used by which page(and which version of that page) Then yo...

Tuan Anh Hoang | Dec 12, 2025

Data Imports in Optimizely: Part 2 - Query data efficiently

One of the more time consuming parts of an import is looking up data to update. Naively, it is possible to use the PageCriteriaQueryService to quer...

Matt FitzGerald-Chamberlain | Dec 11, 2025 |

Beginner's Guide for Optimizely Backend Developers

Developing with Optimizely (formerly Episerver) requires more than just technical know‑how. It’s about respecting the editor’s perspective, ensurin...

MilosR | Dec 10, 2025