user2447986
user2447986

Reputation: 11

how to read query string values in WCF?

This may be very simple but i am not finding a way to read query string value in my WCF rest service. I tried the following but no joy

HttpContext.Current.Request.QueryString["name"]

Upvotes: 1

Views: 69

Answers (1)

Mez
Mez

Reputation: 4726

Something like this should work for you. You need to use the UriTemplate. The following is the WCF service

[ServiceContract]
  interface IPing
  {
    [OperationContract]
    [WebInvoke(Method="POST", UriTemplate="stuff?n={name}&q={quantity}")]
    void AddStuff(string name, string quantity, Stream data);
  }

  class PingService : IPing
  {
    public void AddStuff(string name, string quantity, Stream data)
    {
      Console.WriteLine("{0} : {1}", name, quantity);
      Console.WriteLine("Data ...");
      using (StreamReader sr = new StreamReader(data))
      {
        Console.WriteLine(sr.ReadToEnd());
      }
    }
  }

And the client

 static void Main(string[] args)
    {
      WebRequest req = WebRequest.Create("http://localhost:9000/ping/stuff?n=rich&q=20");
      req.Method = "POST";
      req.ContentType = "text/html";
      using (StreamWriter sw = new StreamWriter(req.GetRequestStream()))
      {
        sw.WriteLine("Hello");
      }

      req.GetResponse();
    }

Upvotes: 1

Related Questions