I am working on a WCF rest service that will need to take in an List of NAme value pairs of values. These values are not known yet, so that is why we need to use a generic list of name value pairs.
I have tried multiple methods, but I am not sure what the best way to do it is. I figured I would be able to access HttpContext and pull the values off the request body, but I am unable to do that.
What is the best way to have a WCF operation that takes in an HTTP Post with a List of Name Value pairs so that they can be read in the operation similar to how you would pull it off the Request["Key"]?
An option would be to create a JSON object to post to the service with a format similar to:
{"kvPairs":[{"Key":"key1","Value":"value1"}, {"Key":"key2","Value":"value2"}]}
On the service side, set up a method similar to the following:
[OperationContract]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "")]
string DoSomething(Dictionary<string, string> kvPairs);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With