I'm developing an application with a WebApiController with a GET method that looks like the following:
public class MyController : ApiController
{
[HttpGet]
[Route("groupIdType/{groupIdType}/groupId/{groupId}")]
public async Task<IHttpActionResult> GetGroup(
[FromUri] string groupIdType,
[FromUri] string groupId)
{
// do stuff...
}
}
The problem is that it's entirely possible that groupId
has URL reserved characters. In fact, for the only currently supported groupIdType
, the groupId
will always have /
characters (I wish I could change this, but it's a requirement that's beyond my control).
I saw this question in the similar questions as I was writing a question about how to pass reserved characters in URL parameters, which suggests encoding the /
character as %2F
in the url. I've already tried that like so:
https://MyServiceBaseUrl/groupIdType/someType/groupId/blah%2Fblah%2Fblah
but this gives me a 404 Not Found. Furthermore, when I tried this:
https://MyServiceBaseUrl/groupIdType/someType/groupId/blah%2F
and inspected the value of groupId
in the debugger, then the value of groupId
is blah
, not blah%2F
or blah/
as I would expect. This makes me think that the encoded slashes are being read as if they weren't encoded.
From a comment on the same question linked above, I got the idea to use this route instead:
[Route("groupIdType/{groupIdType}/groupId/{*groupId}")]
Now, I can send the same URL as the first above, and in the debugger I see that the value of groupId
is blah/blah/blah
- notice that this is not encoded. I get the same result if I pass the unencoded id with this request:
https://MyServiceBaseUrl/groupIdType/someType/groupId/blah/blah/blah
This is actually okay, as some of the other developers on the project expressed concerns about encoding/decoding the id and would rather be able to pass it like this. But I'd also like to understand what's going on here. Why isn't the request going through with the encoded parameters? Also, what does the *
character in the route do? There was no explanation in the question I linked, and I couldn't find anything about it in my searches.