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:
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 !!
Simple but very usefull, exactly what I looking for!!! Thank's.