nabulke nabulke - 3 months ago 58
C# Question

Navigate to uri with umlaut using wpf webbrowser control

I'm using a WPF

WebBrowser
control to navigate to an URI containing a PDF file like that:

XAML

<WebBrowser x:Name="Browser" Loaded="Browser_OnLoaded"/>


Code behind

url = @"file:///c:\A.pdf"; // This works
url = @"file:///c:\Ä.pdf"; // This shows error

Browser.Navigate(url);


Error with Ä.pdf

enter image description here

Question

How can I navigate to the file with umlaut?

I tried UrlEncoding, changing to ASCII encoding, using extended ASCII all without success. Is it possible?




Edit

Using
WebUtility.UrlEncode("Ä");
produces
%C3%84
Why?:

enter image description here

Answer

To make it work with WebBrowser, you must update the encoding. By referencing the System.Web assembly you can use:

  System.Web.HttpUtility.UrlEncode("Ä", Encoding.GetEncoding("ISO-8859-1")));

For below examples, I have used the character Ä.

All, non ASCII characters must be encoded in the URL using percent-encoded characters. It is explained in the following RFC (page 21, last paragraph of section 3.2.2): https://tools.ietf.org/html/rfc3986.

Non-ASCII characters must first be encoded according to UTF-8 [STD63], and then each octet of the corresponding UTF-8 sequence must be percent-encoded to be represented as URI characters.

So the UTF8 character corresponding to Ä is C3 84. Which corresponds to the percent-encoded value: %C3%84.

You can use the following code to encode your file name:

System.Net.WebUtility.UrlEncode("Ä");

or

Uri.EscapeUriString("Ä");