简体   繁体   中英

Html Encode in ASP.NET Web Control

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM