views:

912

answers:

2

I am currently having troubles figuring out how to handle a filepath to be (dynamicly) passed out to a HyperLink control's NavigateUrl property.

Let's say that I'm trying to refer to a file named jäynä.txt at the root of C:.
Passing "file:///C:/jäynä.txt" result to a link to file:///C:/jäynä.txt, as does HttpUtility.UrlPathEncode("file:///C:/jäynä.txt").

Replacing the äs with %E4, which gives the string "file:///C:/j%E4yn%E4.txt", does give a working link to file:///C:/jäynä.txt, but I have not been able to find a way to make the replacement without defining it myself. With Replace("ä", "%E4"), for example.

Is there a way to automaticly handle the filepath string so that the HyperLink would display it correctly, without manualy listing what characters to replace in the string?

Additional Note:
There may be a way to work around this by spesifying the character encoding in which the page is rendered, because debugging shows that the HyperLink at least saves the string "file:///C:/jäynä.txt" unchanged, but somehow mangles it around the time of rendering.
However, this seems only be the case in rendering of the NavigateUrl because other components as well as HyperLink's Text-property are all quite capable of rendering the character ä unchanged.

+1  A: 

This is due to how the browser starts to interpret the path, typically individuals will avoid using characters such as that in the urls of pages.

In your case, I believe you have struck upon the best case scenario, as I am not aware of any way to change the behavior of HttpUtility and/or the NavigateUrl property. At least not without creating a custom control for it.

Mitchel Sellers
No, I do not belive it is the browser. I just checked the source of the page in the first case and the link href is href="file:///C:/j%c3%a4yn%c3%a4.txt", suggesting it's how the server has rendered it in the first place. You may be right on the sollution tough.
DJ Pirtu
It might indirectly be the browser, based on something in BrowserCaps or something similar to render it to the system. Regardless, the values that you specify to the NavigateUrl must be pre-cleaned. That is documented as it does not automatically do anything with UrlEncode.
Mitchel Sellers
+1  A: 

Don't use HyperLink control. Instead use HtmlAnchor control. It will solve your problem. I don't know why Microsoft designed like this.

Syed