1

I need to define a WCF GET method which can retrieve all the query parameters as a single string. Example:

https://xxx.xxx.xxx.xxx/token?client_id=abc_def&client_name=&type=auth&code=xyz

I want to grab the string "client_id=abc_def&client_name=&type=auth&code=xyz".

How do I define the URI template for the method? I tried the following, but it doesn't work as I will get 400 Bad Request. Replacing the /" with "?" makes no difference.

[WebGet(UriTemplate = "token/{Params}")]
[OperationContract]
Stream GetToken(string Params);

The method will call an external service and just forward whatever query parameters that it receives. I don't want individually retrieve each parameters, as it is possible that the parameters may increase.

Another URL would be like this:

https://xxx.xxx.xxx.xxx/person/123456?client_id=abc_def&client_name=&type=auth&code=xyz

In this case, I would like to grab the two strings "123456" and "client_id=abc_def&client_name=&type=auth&code=xyz".

How do I define the URI template for the method?

user3573403
  • 1,780
  • 5
  • 38
  • 64

2 Answers2

0

You can set the expected query string parameters in UriTemplate, like this:

(UriTemplate = "token/{Params}&client_id={clientId}&client_name={clientName}&type={type}&code={code})

And the method can be declared like this:

Stream GetToken(string Params, string clientId, string clienteName, string type, string code);
Ricardo Pontual
  • 3,749
  • 3
  • 28
  • 43
  • Hi, I already know how to do that. But that is not what I want. I want to get the whole query parameters as a single string. As explained in my post, my service will call another external service, passing on the query parameters. I don't want to parse each query parameter. If I am able to retrieve as a single string, there would be no need to change the code if there is change in the query parameters in the future. – user3573403 Feb 11 '17 at 02:31
  • It looks obvious but, have you tried `HttpContext.Current.Request.QueryString`? – Ricardo Pontual Feb 11 '17 at 23:49
  • No. Firstly, how do I specify the URI template for the method? – user3573403 Feb 12 '17 at 00:23
0

You could omit the URI template and use the NameValueCollection from WebOperationContext.Current.IncomingRequest.UriTemplateMatch.QueryParameters Then just make a 'ToString()' call and you will get what you want

String fullQueryParams = WebOperationContext.Current.IncomingRequest.UriTemplateMatch.QueryParameters.ToString()

If you need the path as well, for example to retrieve the person and 123456 strings in the url:

https://xxx.xxx.xxx.xxx/person/123456?client_id=abc_def&client_name=&type=auth&code=xyz

you could use WebOperationContext.Current.IncomingRequest.UriTemplateMatch.RelativePathSegments. Then your OperationContract implementation will be something like this:

String fullQueryParams = WebOperationContext.Current.IncomingRequest.UriTemplateMatch.QueryParameters.ToString();
//fullQueryParams = "client_id=abc_def&client_name=&type=auth&code=xyz"

var pathsCollection = WebOperationContext.Current.IncomingRequest.UriTemplateMatch.RelativePathSegments;
//pathsCollection = ["person","12345"]
Jaime Marín
  • 578
  • 6
  • 11