asp.net-web-api2special-charactersquery-stringmodel-bindingwebapi2

Model binding fails in webapi 2.0


I am using Webapi 2.0. I am passing one parameter having value as vb/c4t+UuRLnQ2W/g8SQ== After model binding i am getting the value of authId in my code as vb/c4t UuRLnQ2W/g8SQ== The (+) sign gets replaced with a space. Could you please help me out how can i get that.

Url: api/employee/1234?authId=vb/c4t+UuRLnQ2W/g8SQ==

[HttpGet]
public IHttpActionResult Get(string eid, string authId)
{
}

Solution

  • + sign has a different meaning in the query string. It is used to represent a space. Another character that has semantic importance in the query string is & which is used to separate the various var=value pairs in the query string.

    Most server side scripts would decode the query parameters before using them, so that a + gets properly converted to a space. Now, if you want a literal + to be present in the query string, you need to specify %2B instead.

    Example yourString.replace("+","%2b")

    Alternative method : You should URLEncode your query string values to make sure you are not loosing the content.

    Another alternate way is like create your own code for + sign. for example 12sfdhjsj8722nsn2232dfsdd will represent a + sign. so you can replace the + sign with the code and in your server side you can get it back using the same code.