DevToolsForYou

URL Encoder and Decoder in C# / .NET — Code Examples

URL Encoder and Decoder in C# / .NETUse the online tool →

URL encoding (percent-encoding) replaces characters that are not allowed in URLs with a percent sign followed by their hexadecimal value. Here is how to encode and decode URL components in each language.

C# provides Uri.EscapeDataString for RFC 3986 encoding and WebUtility.UrlEncode for form-style encoding (spaces as +).

C# / .NET
using System;
using System.Net;

string input = "hello world & foo=bar";

// RFC 3986 percent-encoding (spaces as %20) — for path segments and modern APIs
string encoded = Uri.EscapeDataString(input);
Console.WriteLine(encoded); // hello%20world%20%26%20foo%3Dbar

// Decode
string decoded = Uri.UnescapeDataString("hello%20world%20%26%20foo%3Dbar");
Console.WriteLine(decoded); // hello world & foo=bar

// Form-style encoding (spaces as +) — for HTML form POST bodies
string formEncoded = WebUtility.UrlEncode(input);
Console.WriteLine(formEncoded); // hello+world+%26+foo%3Dbar

string formDecoded = WebUtility.UrlDecode("hello+world+%26+foo%3Dbar");
Console.WriteLine(formDecoded); // hello world & foo=bar

// Build a URL with query parameters
var builder = new UriBuilder("https://example.com/search");
var qs = System.Web.HttpUtility.ParseQueryString(string.Empty);
qs["q"] = "hello world";
qs["page"] = "1";
builder.Query = qs.ToString();
Console.WriteLine(builder.Uri); // https://example.com/search?q=hello+world&page=1
Notes & gotchas
  • Use Uri.EscapeDataString for modern REST APIs and URL path segments (encodes spaces as %20).
  • Use WebUtility.UrlEncode for HTML form data and legacy APIs (encodes spaces as +).
  • System.Web.HttpUtility.ParseQueryString is the cleanest way to build a query string from key-value pairs.
Try it in your browser

Need to url encode/decode without writing code? The URL Encoder and Decoder runs entirely in your browser — paste your input and get the result instantly. No signup, no install, no data sent to a server.

Open URL Encode/Decode
URL Encoder and Decoder in other languages