Passing Named Parameters to POST action and getting JSON array – Programmatically

Yesterday, I was approached by one of my teammate where he was facing an issue with getting JSON response from an ASP.NET MVC action.  He was working on a Console Application where he wanted to get the list of products from another internal ASP.NET MVC website.

This ASP.NET MVC action was taking category (a string argument) and returning an array of products (object with 3 fields) as JSON. Most importantly this was marked as a POST action. QueryString was not an option. I found couple of code samples on the web, where POST data was passed as a byte array.  However - it was always coming as 'null' at the action. I concluded that we need to pass the name of the paramater and its value to get it working. Finally - I found a method in WebClient class called UploadValues which takes NameValueCollection as a parameter. It worked like a charm and we now have a working code where we are invoking a POST action from a console utility and getting JSON data back. Here is the code snippet:


  1.      static void Main(string[] args)
  2.         {
  3.             GetJSonFromPostToUrl("http://mymvcapp/product/category");
  4.             Console.Read();
  5.         }
  6. //It is very important  that you decorate the class and fields with DataContract attribute like below
  7.        [Serializable, XmlRoot("JsonData"), DataContract(Name = "JsonData")]
  8.         public class JsonData
  9.         {
  10.            [XmlElement("ProductId"), DataMember(Name = "ProductId")]
  11.             public string ProductId { get; set; }
  13.            [XmlElement("ProductName"), DataMember(Name = "ProductName")]
  14.             public string ProductName { get; set; }
  16.            [XmlElement("ProductUrl"), DataMember(Name = "ProductUrl")]
  17.             public string ProductUrl { get; set; }
  18.         }
  20.         private static void GetJSonFromPostToUrl(string url)
  21.         {
  22.             WebClient myWebClient = new WebClient();
  23.             myWebClient.Credentials = CredentialCache.DefaultCredentials;
  24.             myWebClient.UseDefaultCredentials = true;
  25.             NameValueCollection myNameValueCollection = new NameValueCollection();
  26.             myNameValueCollection.Add("categoryName", "Electronics");
  27.             Console.WriteLine("\nUploading to {0} ...", url);
  28.             byte[] responseArray =
  29.             myWebClient.UploadValues(url, "POST", myNameValueCollection);
  31.             // Decode and display the response.
  32.             MemoryStream stream = new MemoryStream(responseArray);
  33.             DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(JsonData[]));
  34.             object result = serializer.ReadObject(stream);
  35.             JsonData[] jsonObj = result as JsonData[];
  37.             Console.WriteLine("\nResponse received was :\n{0}", Encoding.ASCII.GetString(responseArray));
  38.         }

Related articles

Comments (2)

  1. Vinay Pradhan says:

    Indeed it worked like a charm !! Thanks for your help Sandeep. Passing the parameter as a namevalue collection was a great idea and a useful learning !!

  2. robertosljunior says:

    Simple but very usefull, exactly what I looking for!!! Thank's.

Skip to main content