SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have custom control which displays different details depending on which site the user is currently at. My problem is that as soon as the URL contains non-alphabetic characters, the controls fails to show any details at all.

Works: http://myserver/subsiteone

Doesn't work: http://myserver/subsite%20two

Snippet of my code

private enum Investor { None, Ventures, Growth, Seeds }
protected override void OnInit(EventArgs e)
    var investor = CheckUrl(SPContext.Current.Site.OpenWeb().Url);

    switch (investor)
        case Investor.None:
            Controls.Add("Some data");
        case Investor.Ventures:
            Controls.Add("Some data");
        case Investor.Growth:
            Controls.Add("Some data");
        case Investor.Seeds:
            Controls.Add("Some data");

private static Investor CheckUrl(string url)
    return url.Contains("Ventures") ? Investor.Ventures : (url.Contains("Growth%20Samples") ? Investor.Growth : (url.Contains("Seeds") ? Investor.Seeds : Investor.None));

As you can see, when I check on Investor.Growth the URL should contain Growth%20Samples, and it does but the code somehow doesn't approve of it as valid and skips on.

How can I prevent this from happening, since it approves the other parameters which have nothing but alphabetic characters in them?

EDIT: This is continuous development on an existing site with tons of subsites, lists etc so a recreation of the site is not possible at this point.

share|improve this question
up vote 1 down vote accepted

Found solution to this.

const string growth = "Growth Sample";
var xEncode = SPHttpUtility.HtmlEncode(growth); 
url.Contains(xEncode) // Taken out of context

I don't know why url.Contains("Growth%20Samples") didn't work, as SPHttpUtility.HtmlEncode gives the exact same output.

Anyhow, enjoy!

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.