2

I have a file called TopicTree.ascx.cs which I am trying to output encoded strings like so:

            string subject = reader.IsDBNull(0) ? string.Empty : reader.GetString(0);
            string topic = reader.IsDBNull(1) ? string.Empty : reader.GetString(1);

            subject = subject.Trim();
            topic = topic.Trim();

            string en_subject = Server.HtmlEncode(subject);
            string en_topic = Server.HtmlEncode(topic);

            string output = string.Format("<li><a href=\"searchresults.aspx?type=topics&subject={1}&topic={2}\" style=\"cursor: pointer;\">{0}</a></li>", topic, en_subject, en_topic);

But when I actually see the output on the screen, it isn't encoded. What's wrong?

1
  • If "output" is supposed to be rendered as HTML, what you are looking for is URLEncode. Commented Aug 12, 2011 at 19:59

1 Answer 1

5

For the link URL, you want Server.UrlEncode() instead of Server.HtmlEncode().

But for the link display, you want Server.HtmlEncode(topic) on the topic as well.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.