David Derman
David Derman

Reputation: 871

C# UrlEncode but allow certain character sets

I have an application where users can upload files whose name can contain non-latin characters such as Cyrillic or Chinese. For example:

http://localhost/привет мир.html

I then need to be able to URL-encode the file name for illegal and/or special characters such as spaces. When I URL-encode a URI as follows:

string uri = "http://localhost/привет мир.html";
string encoded = HttpUtility.UrlPathEncode(uri);
Console.WriteLine(encoded);

Which gives me the following result:

http://localhost/%d0%bf%d1%80%d0%b8%d0%b2%d0%b5%d1%82%20%d0%bc%d0%b8%d1%80.html

What I really want is this:

http://localhost/привет%20мир.html

Most browsers support this format and my users, who in this case are Russian, need to be able to see a more display-friendly URL in their language and character set.

Is there a way to URL-Encode a URI and have it not encode certain character sets?

Upvotes: 3

Views: 1237

Answers (1)

Jason Whitted
Jason Whitted

Reputation: 4024

You will need to create your own HttpEncoder.

HttpEncoder Class - The Remarks section explain how to setup your website to use your HttpEncoder by default in the HttpUtility, HttpServerUtility, and HttpResponseHeader.

Upvotes: 3

Related Questions