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:
- static void Main(string[] args)
- {
- GetJSonFromPostToUrl("https://mymvcapp/product/category");
- Console.Read();
- }
- //It is very important that you decorate the class and fields with DataContract attribute like below
- [Serializable, XmlRoot("JsonData"), DataContract(Name = "JsonData")]
- public class JsonData
- {
- [XmlElement("ProductId"), DataMember(Name = "ProductId")]
- public string ProductId { get; set; }
- [XmlElement("ProductName"), DataMember(Name = "ProductName")]
- public string ProductName { get; set; }
- [XmlElement("ProductUrl"), DataMember(Name = "ProductUrl")]
- public string ProductUrl { get; set; }
- }
- private static void GetJSonFromPostToUrl(string url)
- {
- WebClient myWebClient = new WebClient();
- myWebClient.Credentials = CredentialCache.DefaultCredentials;
- myWebClient.UseDefaultCredentials = true;
- NameValueCollection myNameValueCollection = new NameValueCollection();
- myNameValueCollection.Add("categoryName", "Electronics");
- Console.WriteLine("\nUploading to {0} ...", url);
- byte[] responseArray =
- myWebClient.UploadValues(url, "POST", myNameValueCollection);
- // Decode and display the response.
- MemoryStream stream = new MemoryStream(responseArray);
- DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(JsonData[]));
- object result = serializer.ReadObject(stream);
- JsonData[] jsonObj = result as JsonData[];
- Console.WriteLine("\nResponse received was :\n{0}", Encoding.ASCII.GetString(responseArray));
- }
Related articles
- Getting JSON-serialized data using Ajax and jQuery in ASP.NET MVC (johnolamendy.wordpress.com)
- Security consideration when passing data through Ajax request in ASP.NET MVC (dashingquill.mlblogs.com)
- ASP.NET MVC - How to best build a form action that can respond in many ways? (stackoverflow.com)
- how to create multidimensional array as JSON for jqPlot chart in ASP.NET MVC C# (stackoverflow.com)
- Getting JSON feed data from RESTFul web service (stackoverflow.com)
Comments
Anonymous
December 01, 2011
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 !!Anonymous
August 12, 2012
Simple but very usefull, exactly what I looking for!!! Thank's.