Winforms WebControl - How can I prevent escaping of links?

137 Views Asked by At

I am developing in a Winforms .NET 4.0 project, using a WebControl as a WYSIWYG editor - working from the YARTE editor developed by Matt Groves.

I am trying to add an anchor tag and set the href attribute to the following path:

var path = http://someurl.aspx?param1="val1"&param2="val2"&param3="youGetTheIdea"

I have tried several approaches; I always get HTML-escaped ampersands when I try to write the url to the document:

http://someurl.aspx?param1="this"&param2="doesnt"&param3="work"

Approaches I Have Tried Unsuccessfully:

  1. Creating the Link

    webBrowser.ExecCommand("CreateLink", false, path)
    
  2. Creating the HTML and pasting it in:

    var htmlDocument2 = args.Document.DomDocument as IHTMLDocument2;
    if (htmlDocument2 == null) return;
    var range = htmlDocument2.selection.createRange() as IHTMLTxtRange;
    if (range == null) return;
    range.pasteHTML(string.Format(path, range.text));
    
  3. Creating a file and directing the webBrowser to it:

    // assume the links are already inserted, but aren't right.
    var textWithBadLinks = webBrowser.DocumentText;
    var betterText = UseRegexToReplaceBadLinkText(textWithBadLinks);
    using (StreamWriter outfile =new StreamWriter(@"c:\test.html"))
    {
      outfile.Write(betterText);
    }
    webBrowser.Url= new Uri(@"c:\test.html");
    
  4. Creating a stream and directing the webBrowser to it:

    // same as above, but instead of the URL, use the DocumentStream:
    webBrowser.DocumentStream = new StreamWriter(@c:\test.html);
    
  5. Navigating to the file:

    webBrowser.Navigate(new Uri(@"c:\test.html"))
    

Regardless of the approach I choose, the ampersands get escaped the links don't work.

Thank you in advance for any assistance.

1

There are 1 best solutions below

0
On BEST ANSWER

Technically speaking, XHTML requires &amp to be valid. See: XHTML and & (Ampersand) encoding