05 Nov, 2017 · 1 minute read
There are several ways you can encode URLs in C#. It all depends on what framework you are using.
If you are using .NET Core (either ASP .NET, a Class library or a Console App) or even .NET Standard, you can use one of these two methods:-
System.Web.HttpUtility.UrlEncode
OR
System.Net.WebUtility.UrlEncode
If you are inside an instance method of System.Web.Mvc.Controller, you can use the Server property as follows:-
Server.UrlEncode
For any other class, you can use one of these:-
System.Web.HttpUtility.UrlEncode
OR
System.Net.WebUtility.UrlEncode
For the remaining portions of the .NET platform like a .NET Framework Console Application, Class Library, Windows Service & Desktop Application you can use the following:-
System.Net.WebUtility.UrlEncode
On a side note, you can try my online URL Encoder.
Click hereto read my blog post on how to decode URL in C#.