Nick Gallimore
Nick Gallimore

Reputation: 1263

Parameter Binding in ASP.NET Web API Decoding + (%2B) As Space

Given the following method in a controller for a ASP.NET Web API:

[HttpGet]
[ApiRoute("resource/{id}/end-point)]
public IHttpActionResult MethodName (int id, string clientTimeZone)
{
     ...
}

Whenever I submit a GET request to http://localhost:5684/api/v1/resource/1/end-point?client_timezone=%2B0500 clientTimezone is passed in to clientTimeZone as %2B0500 and it parses the encoded '+' sign into a space character. Why can't ASP.NET decode +'s from the URI?

In the header I have "ContentType = application/json" and a bearer token

I am trying to get "+0500" into my method but it is turning into " 0500"

Upvotes: 10

Views: 1156

Answers (1)

Anthony Greer
Anthony Greer

Reputation: 66

Are you using Content-Type of application/x-www-form-urlencoded when consuming the api? This will treat a '+' character as a space when used in your URL.

More details here: When to encode space to plus (+) or %20?

Try changing your Content-Type to application/json instead and see if parameter binding behaves as expected.

Upvotes: 3

Related Questions