문제

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 use HttpUtility.ParseQueryString to parse that into a NameValueCollection, which is similar to a Dictionary<string, string> that you want.

다른 팁

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top