HttpContext.Current.Server.UrlEncode
This does only work in .NET Framework. How can I encode or decode URI arguments in ASP.NET Core?
HttpContext.Current.Server.UrlEncode
This does only work in .NET Framework. How can I encode or decode URI arguments in ASP.NET Core?
For ASP.Net Core 2.0+ and if you need spaces to be encoded as %20
as opposed to +;
Use:
Uri.EscapeDataString(someString);
Just adding another approach to the mix:
UrlEncoder.Default.Encode(url);
https://docs.microsoft.com/en-us/dotnet/api/system.text.encodings.web.urlencoder?view=net-6.0
I'm using a redirect, and UrlEncode did not work for me because it encodes the entire url. I solved this by instead using UriHelper.Encode, shown below.
// generate url string...
return Redirect(Microsoft.AspNetCore.Http.Extensions.UriHelper.Encode(new System.Uri(url)));
Don't waste your time, I've got plenty of experience with these so called url encoders, they are all useless, and have different quirks. Eg WebUtility.UrlEncode doesn't take care of "+" sign.
If you want to encode URL parameters, employ a BASE58 encoding. It uses only alphabet letters + numbers, thus you don't need to url encode.