I want to create a WCF RESTful service method that can receive an arbitrary number of parameters in the query string and send them to some type of key,value collection parameter on the method. For Example:
if I call
"http://localhost/Service.svc/DoWork?p1=test&p2=testAgain"
I would like the method implementation to look like:
[WebGet]
public void DoWork(Dictionary<string,string> values)
{
// Add your operation implementation here
return;
}
and the values
parameter would contain p1, p2 keys with the respective values.
How can I do this? I was thing one way would be to define a custom UriTemplate and have everything sent to one string parameter and parse it out but the "&" characters are throwing and dangerous forum request exception. I can't help but think there is a simpler way to achieve this.
You can access the request query string via
WebOperationContext.Current.IncomingRequest.UriTemplateMatch.RequestUri.Query
. You can then useHttpUtility.ParseQueryString
to parse that into aNameValueCollection
, which is similar to aDictionary<string, string>
that you want.