Question

Using ASP.NET Web API, is there a way for me to pass in an arbitrary number of parameters using the URI in the same fashion as they are input to a console application using the args keyword. In the Web API case, the parameters would be separated by forward slashes (/). So let's say I have a method in a controller MessageController such as this:

public string Get(params string[] args)
{
    string returnValue = "";

    for (int i=0;i < args.Length; i++)
    {
        returnValue += args[i] + " ";
    }

    return returnValue;
}

I would like to go to:

../api/Message/a/b/c/d/e/f/g/h/i

and it should return the string:

a b c d e f g h i

The tricky part is getting the parameters to bind to the type string[] instead of providing an object with multiple string properties. Does anyone know of a way to accomplish this? Thank you.

EDIT: The params keyword was left in there but I'm not sure if Web API routing knows how to handle params.

Was it helpful?

Solution

Here is a great blog post by Tugberk that shows how to do it:

http://www.tugberkugurlu.com/archive/asp-net-web-api-catch-all-route-parameter-binding

In short you use a combination of a catch all route + HttpParameterBinding.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top