简体   繁体   English

GET上的WCF WebGet字典参数

[英]WCF WebGet Dictionary Parameter on GET

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. 我想创建一个WCF RESTful服务方法,它可以在查询字符串中接收任意数量的参数,并将它们发送到方法上的某种类型的键,值集合参数。 For Example: 例如:

if I call 如果我打电话

"http://localhost/Service.svc/DoWork?p1=test&p2=testAgain" 的 “http://localhost/Service.svc/DoWork P1 =试验&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. values参数将包含具有相应值的p1,p2键。

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. 我有一种方法是定义一个自定义的UriTemplate并将所有内容发送到一个字符串参数并解析它,但“&”字符抛出和危险的论坛请求异常。 I can't help but think there is a simpler way to achieve this. 我不禁想到有一种更简单的方法来实现这一目标。

你能从HttpContext.Current.Request.QueryString对象中提取这些信息吗?

You can access the request query string via WebOperationContext.Current.IncomingRequest.UriTemplateMatch.RequestUri.Query . 您可以通过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. 然后,您可以使用HttpUtility.ParseQueryString将其解析为NameValueCollection ,它类似于您想要的Dictionary<string, string>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM