3

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?

1
  • Unless you intend for + to mean " ", then you should encode your string. Commented Sep 14, 2019 at 11:05

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

Comments

1

We ran into encoding issues with plus signs in url parameters and the fix for us was to use the following.

Uri.EscapeDataString(urlParameter);
Uri.UnescapeDataString(escapedUrlParameter);

Wanted to share this in case anyone else runs into the same issue.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.