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
Uri.EscapeDataString (recommended for data)
This method encodes everything that is not an unreserved character per RFC 3986, including /:
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.
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:
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
| Method | Namespace | Encodes /? | .NET Core | Best For |
Uri.EscapeDataString | System | Yes (%2F) | Yes | Path segments, RFC-strict encoding |
WebUtility.UrlEncode | System.Net | Yes (%2F), spaces as + | Yes | Query string values |
HttpUtility.UrlEncode | System.Web | Yes (%2f), spaces as + | No (requires package) | Legacy ASP.NET WebForms |
Uri.EscapeUriString | System | No | Yes | Full 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:
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:
UriBuilder paths
UriBuilder does not encode path components for you. Combine it with Uri.EscapeDataString:
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:
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.EscapeDataStringon an already encoded string turns%2Finto%252F. Encode raw values only. - Using
Uri.EscapeUriStringfor values: it is meant for full URIs and does not encode reserved characters like/,?or#. Microsoft deprecated it in .NET 6. - Mixing
UrlEncodeandUrlPathEncode: they produce different results for the same input. PreferUri.EscapeDataStringfor predictable output. - Forgetting the server: correct encoding in code does not help if the server or a proxy blocks or decodes
%2Fin the path. - Hex digit case:
%2Fand%2fmean the same thing per RFC 3986, but some poorly implemented servers treat them differently.Uri.EscapeDataStringalways produces uppercase hex, the recommended form.
Related reading
- URL Encoding using C
- Usage of ConfigureAwait in .NET
- Usage of EnsureSuccessStatusCode and handling of HttpRequestException it throws
- Use AdaBoostBoosting with Accord.Net
- Use cases for async void methods, revisited
- Use of await keyword in c
- Use of Finalize/Dispose method in C
- User authentication and authorisation in ASP.NET MVC

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseData 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.