.NET
URL Encoding
Forward Slash
Web Development
Programming

URL encode a forward slash (/) in .NET

Codemia Knowledge Hub ·

To URL-encode a forward slash in .NET, use Uri.EscapeDataString("/"), which produces %2F. WebUtility.UrlEncode and HttpUtility.UrlEncode also encode /, but they use form-style rules (spaces become +) rather than RFC 3986, so pick the method based on whether the slash is data or a path delimiter.

Why a slash in data needs encoding

URL encoding (percent-encoding) replaces unsafe or reserved characters with a % followed by two hex digits. RFC 3986 defines the forward slash / as a reserved path delimiter. When a slash is part of a value rather than a path separator, leaving it unencoded makes servers misread the URL structure.

For example, passing the date 04/01/2023 as a route parameter without encoding produces /api/dates/04/01/2023, which the server reads as three path segments instead of one value.

The three encoding methods in .NET

This method encodes everything that is not an unreserved character per RFC 3986, including /:

csharp
1string date = "04/01/2023";
2string encoded = Uri.EscapeDataString(date);
3Console.WriteLine(encoded);
4// Output: 04%2F01%2F2023

Use it when the value goes into a URL path segment or query parameter and its slashes are data, not delimiters.

WebUtility.UrlEncode (.NET Core / .NET 5+)

WebUtility.UrlEncode lives in System.Net. It leaves only letters, digits and -, _, ., !, *, (, ) unencoded, so a slash becomes %2F and a space becomes +. Microsoft's documentation notes that this character set matches neither the URL specification nor RFC 3986 and recommends EscapeDataString for RFC-compliant encoding.

csharp
1string date = "04/01/2023";
2string encoded = System.Net.WebUtility.UrlEncode(date);
3Console.WriteLine(encoded);
4// Output: 04%2F01%2F2023

The method suits form-style query string values in modern .NET applications.

HttpUtility.UrlEncode (legacy System.Web)

HttpUtility.UrlEncode lives in System.Web. It uses the same safe character set as WebUtility.UrlEncode, so it encodes /, but it writes lowercase hex digits:

csharp
1string date = "04/01/2023";
2string encoded = System.Web.HttpUtility.UrlEncode(date);
3Console.WriteLine(encoded);
4// Output: 04%2f01%2f2023   (lowercase hex)

The related HttpUtility.UrlPathEncode method behaves differently (it turns spaces into %20), so mixing the two is a common source of inconsistent URLs.

Comparison table

MethodNamespaceEncodes /?.NET CoreBest For
Uri.EscapeDataStringSystemYes (%2F)YesPath segments, RFC-strict encoding
WebUtility.UrlEncodeSystem.NetYes (%2F), spaces as +YesQuery string values
HttpUtility.UrlEncodeSystem.WebYes (%2f), spaces as +No (requires package)Legacy ASP.NET WebForms
Uri.EscapeUriStringSystemNoYesFull URIs (rarely what you want)

Encoding values in real requests

A path parameter in a REST API

When a value containing slashes is part of a URL path, encode it:

csharp
1string filePath = "documents/reports/q1.pdf";
2string apiUrl = $"https://api.example.com/files/{Uri.EscapeDataString(filePath)}";
3Console.WriteLine(apiUrl);
4// Output: https://api.example.com/files/documents%2Freports%2Fq1.pdf

Without encoding, the server tries to match /files/documents/reports/q1.pdf against your routes and likely returns a 404.

A query string value

Slashes are usually safe in query parameters, but encoding them removes ambiguity:

csharp
1string searchTerm = "TCP/IP networking";
2string queryUrl = $"https://example.com/search?q={Uri.EscapeDataString(searchTerm)}";
3Console.WriteLine(queryUrl);
4// Output: https://example.com/search?q=TCP%2FIP%20networking

UriBuilder paths

UriBuilder does not encode path components for you. Combine it with Uri.EscapeDataString:

csharp
1var builder = new UriBuilder("https://api.example.com");
2string folder = "my/folder";
3builder.Path = $"/api/resources/{Uri.EscapeDataString(folder)}";
4Console.WriteLine(builder.Uri);
5// Output: https://api.example.com/api/resources/my%2Ffolder

Servers may still reject %2F in paths

Correct encoding in code does not guarantee the server accepts it. Depending on the hosting stack and its configuration, IIS, ASP.NET or a reverse proxy in front of the app can reject a path containing %2F with a 400 or 404, or decode it to / before routing so the value splits into separate segments again. The exact behavior and the settings that change it differ between IIS, http.sys and .NET versions, so test a request with an encoded slash against your real deployment.

Note that IIS request filtering's allowDoubleEscaping setting is about double-escaped sequences such as %252F, not a single %2F, so turning it on is not the fix for this case.

When you control the API, the most portable option is to avoid slashes in path segments: send the value as a query string parameter, or use a catch-all route parameter ({*path} in ASP.NET Core) that is meant to contain slashes.

Decoding %2F back to a slash

Use the matching decode method:

csharp
1string encoded = "04%2F01%2F2023";
2string decoded = Uri.UnescapeDataString(encoded);
3Console.WriteLine(decoded);
4// Output: 04/01/2023

For path segments, decode each component on its own rather than the whole URL, so a decoded %2F is not mistaken for a path separator.

Mistakes that break slash encoding

  • Double encoding: running Uri.EscapeDataString on an already encoded string turns %2F into %252F. Encode raw values only.
  • Using Uri.EscapeUriString for values: it is meant for full URIs and does not encode reserved characters like /, ? or #. Microsoft deprecated it in .NET 6.
  • Mixing UrlEncode and UrlPathEncode: they produce different results for the same input. Prefer Uri.EscapeDataString for predictable output.
  • Forgetting the server: correct encoding in code does not help if the server or a proxy blocks or decodes %2F in the path.
  • Hex digit case: %2F and %2f mean the same thing per RFC 3986, but some poorly implemented servers treat them differently. Uri.EscapeDataString always produces uppercase hex, the recommended form.

Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms