URL Encoding using C#
Edit: Note that this answer is now out of date. See Siarhei Kuchuk's answer below for a better fix
UrlEncoding will do what you are suggesting here. With C#, you simply use HttpUtility
, as mentioned.
You can also Regex the illegal characters and then replace, but this gets far more complex, as you will have to have some form of state machine (switch ... case, for example) to replace with the correct characters. Since UrlEncode
does this up front, it is rather easy.
As for Linux versus windows, there are some characters that are acceptable in Linux that are not in Windows, but I would not worry about that, as the folder name can be returned by decoding the Url string, using UrlDecode
, so you can round trip the changes.
How to URL encode strings in C#
According to RFC 1738:
Thus, only alphanumerics, the special characters "$-_.+!*'(),", and
reserved characters used for their reserved purposes may be used
unencoded within a URL.
Neither HttpUtility.UrlEncode
nor WebUtility.UrlEncode
will encode those characters since the standard says the parentheses ()
can be used unencoded.
I don't know why the URL Encoder / Decoder you linked encodes them since it also lists them as as a character that can be used in a URL.
C# method to do URL encoding?
HttpUtility.UrlEncode
URL encode in C#
HttpUtility.UrlEncode
does the right thing here.
Encodes a URL string. The UrlEncode method can be used to encode the entire URL, including query-string values.
When it comes to spaces on the URL, a +
or %20
are both correct.
Also see URL encoding the space character: + or %20?.
UTF-8 URL Encode
The method HttpUtility.ParseQueryString
internally returns a HttpValueCollection
. HttpValueCollection.ToString()
already performs url encoding, so you don't need to do that yourself. If you do it yourself, it is performed twice and you get the wrong result that you see.
I don't see the relation to UTF-8. The value you use ({"mbox":"mailto: UserName@company.com"}) doesn't contain any characters that would look different in UTF-8.
References:
- HttpValueCollection and NameValueCollection
- ParseQueryString source
- HttpValueCollection source
Create new Uri using Url encoded string in C#
Based on locale settings and used keyboard, iOS may use some other apostrophe-like character, for example right single quotation mark U+2019 (').
Solution was to handle that.
URL Encode and Decode in ASP.NET Core
For ASP.NET Core 2.0+ just add
System.Net
namespace -WebUtility
class is shipped as part ofSystem.Runtime.Extensions
nuget package, that is referenced by default in ASP.NET Core project.For the previous version add
Microsoft.AspNetCore.WebUtilities
nuget package.
Then the WebUtility
class will be available for you:
public static class WebUtility
{
public static string UrlDecode(string encodedValue);
public static string UrlEncode(string value);
}
Related Topics
Filtering Datagridview Without Changing Datasource
List≪T≫ or Ilist≪T≫
How to Save/Restore Serializable Object To/From File
Comparing Object Properties in C#
How to Make a Winforms App Go Full Screen
How to Retrieve Id of Inserted Entity Using Entity Framework
Sending Http Requests in C# With Unity
How to Delete an Element from an Array in C#
How to Speed Up the Build and Run Process in Unity for Mobile Devices iOS/Android
How to Update an Observablecollection Via a Worker Thread
Setting Authorization Header of Httpclient
String Output: Format or Concat in C#
Dynamically Replace the Contents of a C# Method