Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"

like image 662
Nick Gallimore Avatar asked Apr 13 '18 15:04

Nick Gallimore


People also ask

What is parameter binding in ASP NET?

The Parameter Binding in ASP.NET Web API means how the Web API Framework binds the incoming HTTP request data (query string or request body) to the parameters of an action method of a Web API controller. The ASP.NET Web API action methods can take one or more parameters of different types.

How to bind a parameter to a type in web API?

…… Another way is to add ModelBinder attribute to the type. When we define ModelBinder attribute to the type, Web API uses this model binder for all the parameters of this type. Model binding is a very important process to bind parameter with the value.

How does ASP NET Web API get the value of a parameter?

You have seen that by default, ASP.NET Web API gets the value of a primitive parameter from the query string and a complex type parameter from the request body. But, what if we want to change this default behavior?

What is the use of binding in web API?

Binding is a process to set values for the parameters when Web API calls a controller action method. In this article, we learn how to map Web API methods with the different types of the parameters and how to customize the binding process. Web API tries to get the value from the URI.


1 Answers

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.

like image 153
Anthony Greer Avatar answered Oct 06 '22 00:10

Anthony Greer