Retreive WCF query parameters as a single string

1.4k Views Asked by At

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?

2

There are 2 best solutions below

3
Ricardo Pontual On

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);
0
Jaime Marín On

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"]