Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plus sign in query parameter becomes empty string (ASP.NET Core)

In my application I want to add sorting like that: 'https://example.com/entity?sort_by=+property'.

When I try to pass plus sign to query, ASP.NET Core returns 'property' without plus sign. How can I fix this?

like image 792
Sergo Avatar asked May 31 '26 22:05

Sergo


1 Answers

The plus symbol is a reserved character according to RFC3986 which is the standard specification for URIs and it is used to represent a space, therefore, if you want to use it in your URL as literally the "+" character instead of a space, you need to encode it so it looks like:

https://example.com/entity?sort_by=%2Bproperty

In ASP.NET Core you can encode / decode URL's as follows:

using System.Net;

string url = "https://example.com/entity?sort_by=+property";

string encodedUrl = WebUtility.UrlEncode(url);
string decodedUrl = WebUtility.UrlDecode(encodedUrl);

See here for more info about URL encoding.

like image 131
Isma Avatar answered Jun 02 '26 10:06

Isma