How to Urlencode Without Using System.Web

How do you UrlEncode without using System.Web?

System.Uri.EscapeUriString() can be problematic with certain characters, for me it was a number / pound '#' sign in the string.

If that is an issue for you, try:

System.Uri.EscapeDataString() //Works excellent with individual values

Here is a SO question answer that explains the difference:

What's the difference between EscapeUriString and EscapeDataString?

and recommends to use Uri.EscapeDataString() in any aspect.

How to UrlDecode without having System.Web dll in c#

//.Net 4.0+

you can use WebUtility.UrlDecode(). It is not part of system.web.

And you can see this blog:
Html and Uri String Encoding without System.Web

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.

Replacement for System.Web.HttpUtility.UrlEncode/UrlDecode ASP.NET 5

System.Runtime.Extensions defines both UrlDecode and HtmlDecode.

namespace System.Net
{
public static partial class WebUtility
{
public static string HtmlDecode(string value) { return default(string); }
public static string HtmlEncode(string value) { return default(string); }
public static string UrlDecode(string encodedValue) { return default(string); }
public static byte[] UrlDecodeToBytes(byte[] encodedValue, int offset, int count) { return default(byte[]); }
public static string UrlEncode(string value) { return default(string); }
public static byte[] UrlEncodeToBytes(byte[] value, int offset, int count) { return default(byte[]); }
}
}

Update

While System.Runtime.Extensions defines the extension, as you can notice from it's code the actual class you need to call is System.Net.WebUtility

Option 1: System.Net.WebUtility

  • Documentation
  • Source Code

Currently there are no publicly made plans to include Decode in Microsoft.Framework.WebEncoders.

Usage

System.Net.WebUtility.UrlEncode(myString)
System.Net.WebUtility.UrlDecode(myString)

Option 2: System.Text.Encodings.Web.UrlEncoder

  • Source Code

This is registered in the asp.net core service container and is injectable into your controllers etc.

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 of System.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);
}

System.Net.WebUtility.UrlEncode and System.Web.HttpUtility.UrlEncode methods differences

Is it expected?

Looks like it is expected. Going through the source code WebUtility I see that public UrlEncode calls one of the private one

    public static string UrlEncode(string value)
{
// code stripped
return Encoding.UTF8.GetString(UrlEncode(bytes, 0, bytes.Length, false /* alwaysCreateNewReturnValue */));
}

//private one makes a call to IntToHex
private static byte[] UrlEncode(byte[] bytes, int offset, int count)
{
expandedBytes[pos++] = (byte)IntToHex((b >> 4) & 0xf);

// IntToHex casting to uppercase character, reason being every
// encoded character is returning as uppercase.
private static char IntToHex(int n)
{
//code stripped
else
return (char)(n - 10 + (int)'A'); //here
}


Related Topics



Leave a reply



Submit