1
votes

I'm trying to build a get action in WebAPI (C#) which will respond to the following:

http://{host}/api/Callback/CompleteFlow/{MyId}/{dynamic parameters I have no control on}

For example: http://{host}/api/Callback/CompleteFlow/7?param1=abc&param2=def

and also: http://{host}/api/Callback/CompleteFlow/7

I've created an action with the following signature:

[HttpGet]
public HttpResponseMessage CompleteFlow(int MyId, string requestParams)
{
}

and added specific routing:

config.Routes.MapHttpRoute(
                name: "Callback",
                routeTemplate: "api/Callback/CompleteFlow/{MyId}/{*requestParams}",
                defaults: new { controller = "Callback", action = "CompleteFlow", requestParams = RouteParameter.Optional });

I'm getting MyId parameter but not the dynamic parameters into the requestParams parameter within the action (I'm expecting to get requestParams=param1=abc&param2=def, but gets null instead).

What am I missing here?

Thanks, Nir.

1

1 Answers

3
votes

Managed to fix this using the following approach:

[HttpGet]
public HttpResponseMessage CompleteFlow(int MyId)
{
    var queryString = this.Request.GetQueryNameValuePairs();
}

config.Routes.MapHttpRoute(
                name: "Callback",
                routeTemplate: "api/Callback/CompleteFlow/{MyId},
                defaults: new { controller = "Callback", action = "CompleteFlow" });

Hope it helps somebody :)

Thanks, Nir.