I have a very simple webservice with one GET method that has a token string parameter. I use this token can have letters, "+" and "/". Ex.: mC1SW7RJhgsQGtRHhsg/Q+FGoZjbBleNKtpqT7zLMtE
I use this token to look in the database if there is data for this token.
My method in the controller is like that:
[HttpGet("{token}")]
[ProducesResponseType(typeof(string), 200)]
[ProducesResponseType(typeof(void), 404)]
public JsonResult Get(string token)
{
string sql = "SELECT dataObject FROM Session WHERE id = @SessionToken";
var data = _conn.QueryFirstOrDefault<Session>(sql, new {SessionToken = token});
if (data == null){
var r = Json(string.Empty);
r.StatusCode = StatusCodes.Status404NotFound;
return r;
}
else {
return Json(JsonConvert.DeserializeObject(data.dataObject));
}
}
I see Dapper/Webapi automatically escapes this parameter and change "/" to "%2F" for example.
When I deploy it just works for tokens without special characters and return 404.
As a workaround I changed the token in the server to encode the token and replace the encoded plus sign to space:
string decodedToken = WebUtility.UrlDecode(token);
token = decodedToken.Replace(" ", "+");
The problem is that I need my clients to do the inverse and replace the '+' sign:
var encodedToken = WebUtility.UrlEncode(token);
// Convert '+' to ' '
token = encodedToken.Replace("%2B", " ");
What is the recommended way to work without asking the client to replace de '+' signs?