1

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.

Community
  • 1
  • 1
wlyles
  • 2,236
  • 1
  • 20
  • 38
  • You may need to write RegEx to match the acceptable parameter format: The attribute would look like this for example: `{groupId:regex(^\d{3}-\d{3}-\d{4}$)}` (obviously that isn't the RegEx, but it is for example/format sake.) – Mark C. Dec 07 '16 at 19:43
  • The framework decodes parameter values as a convenience so at the end you won't have to decode it yourself. Is it a problem that the values aren't encoded? You could just UrlEncode it yourself if you need to – Dustin Hodges Dec 07 '16 at 20:16
  • The `*` essentially means "this variable needs to accept multiple URL segments as one string". It is known as a *catch-all parameter*. See "[Handling a Variable Number of Segments in a URL Pattern](https://msdn.microsoft.com/en-us/library/cc668201.aspx#Anchor_5)" in the ASP.Net Routing documentation. – Brian Rogers Dec 07 '16 at 22:53

0 Answers0