How to prevent HtmlGenericControl from html content encoding?

I am writing an asp.net website (in fact, the DotNetNuke module) using C #. Due to the code, I am trying to create a <script> tag where I set the required js variable and then add it to the HMTL <head> . Js var is a (literal) array of relative URLs for image files. Because the array contains strings, each element must be enclosed in quotation marks.

The problem is that the line between <script> and </script> automatically placed in HtmlEncoded, so the quotation marks around each element of the array are replaced with &quot; . This happens when an HtmlGenericControl displayed. Could DotNetNuke be the culprit? Can anyone suggest a workaround?

My current code (works from the Page_Load handler in my control):

 HtmlGenericControl PreviewDataScriptTag = new HtmlGenericControl("script"); PreviewDataScriptTag.Attributes.Add("type", "text/javascript"); StringBuilder PreviewDataScriptCode = new StringBuilder(); PreviewDataScriptCode.Append("var preview_imgs = ["); string pathPrefix = @""""; string pathSuffix = @""","; foreach (string path in this.DocPreviewImages) { PreviewDataScriptCode.Append(pathPrefix + PreviewUrlBase + Path.GetFileName(path) + pathSuffix); } // Remove last comma from js code PreviewDataScriptCode.Remove(PreviewDataScriptCode.Length-1, 1); PreviewDataScriptCode.Append("];"); PreviewDataScriptTag.InnerText = PreviewDataScriptCode.ToString(); Page.Header.Controls.Add(PreviewDataScriptTag); 
+6
source share
1 answer

Look at the InnerHtml property of the node property, not the InnerText property.

The InnerHtml property does not automatically encode special characters in and out of HTML objects. HTML objects allow you to display special characters, such as <character, which the browser usually interpreted as having special meaning. <the character will be interpreted as the beginning of the tag and will not be displayed on the page. To display the <character, you will need to use the <object.

+14
source

Source: https://habr.com/ru/post/895451/


All Articles