WCF WebGet Dictionary Parameter on GET

1.5k views Asked by At

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.

2

There are 2 answers

0
carlosfigueira On BEST ANSWER

You can access the request query string via WebOperationContext.Current.IncomingRequest.UriTemplateMatch.RequestUri.Query. You can then use HttpUtility.ParseQueryString to parse that into a NameValueCollection, which is similar to a Dictionary<string, string> that you want.

0
Kellen On

Could you pull this information out of the HttpContext.Current.Request.QueryString object?