How do you UrlEncode without using System.Web?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
If you do not want to reference System.Web, you still have several valid ways to URL-encode data in .NET. The correct choice depends on whether you are encoding a query parameter value, a full URI component, or form-style content. In most modern code, Uri.EscapeDataString is the safest built-in replacement.
Use Uri.EscapeDataString for Query Parameter Values
When you need to encode a single value that will go into a query string, Uri.EscapeDataString is usually the right API.
This produces percent-encoded output suitable for embedding in a URL query parameter value.
Build a full query string safely like this:
Encode values, not the full key=value&key2=value2 structure in one call.
Understand EscapeDataString Versus EscapeUriString
These two APIs are not interchangeable.
- '
EscapeDataStringis for individual data components' - '
EscapeUriStringis for a broader URI string and is not ideal for arbitrary query values'
For query parameters, path fragments, or any user-provided piece of a URL, prefer EscapeDataString.
This protects reserved characters that would otherwise alter URL meaning.
If You Need Form-Style Encoding
Some systems expect the application/x-www-form-urlencoded behavior where spaces become + instead of %20. EscapeDataString does not do that automatically.
Simple helper:
Use this only when the receiving system explicitly expects form-style encoding.
Use WebUtility.UrlEncode When Available
If your goal is only avoiding System.Web, another built-in option in many .NET environments is System.Net.WebUtility.
This is often easier than writing custom logic and does not require the full System.Web dependency.
Avoid Writing a Full Manual Encoder Unless You Must
Manually encoding every byte yourself is possible, but it is easy to get wrong for Unicode and reserved characters. If you absolutely need custom behavior, operate on UTF-8 bytes explicitly.
This should be a last resort, not a default approach.
Encode Only the Correct URL Part
A frequent bug is encoding an entire URL instead of only the dynamic component. For example, this is wrong:
That encodes characters like : and / that are part of the URL structure. Instead, keep the structure literal and encode only the value:
Common Pitfalls
One common mistake is using EscapeUriString when the actual need is encoding a query parameter value. Another is encoding an entire URL and breaking its structure. Developers also mix %20 and + semantics without checking which encoding the receiver expects. Hand-written encoders often mishandle Unicode because they work on characters instead of UTF-8 bytes. Finally, teams sometimes reimplement encoding even though Uri.EscapeDataString or WebUtility.UrlEncode already solves the problem.
Summary
- You do not need
System.Webto URL-encode values in .NET. - Use
Uri.EscapeDataStringfor most query parameter and URI component encoding. - Use
WebUtility.UrlEncodewhen available and appropriate. - Only use form-style
+replacement when the receiver expects it. - Encode URL components, not whole URLs.
- Avoid custom encoders unless you have a clearly defined nonstandard requirement.

