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"
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.
…… 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.
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?
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With