Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<blockquote> <p>From what I understand, a web api controller will automatically convert the json request and map the parameters, so is it best to create a custom class</p> </blockquote> <p>Yes, If the data passed with the request can be mapped to the object, Web API will convert the JSON or XML object to the class object. </p> <p>If you want your json data to come inside an object to your method then first define a class which will hold your json data. </p> <pre><code>public class UserListRequest { public int UserId { get; set; } public string CsvListOfIds { get; set; } } </code></pre> <p>Later modify your method signature to receive object of that class :</p> <pre><code>[HttpPost] public List&lt;User&gt; GetUserList(UserListRequest obj) { List&lt;User&gt; list = new List&lt;User&gt;(); list.Add(new User { ID = obj.UserId }); list.Add(new User { ID = obj.UserId + 1 }); //something return list; } </code></pre> <p>Remember to use <code>[HttpPost]</code> attribute. Also there should be one post method you may comment out the already provided <code>Post</code> method in the controller. (If you want to have multiple post methods inside your controller then see <a href="https://stackoverflow.com/questions/11407267/multiple-httppost-method-in-mvc4-web-api-controller">this question</a>)</p> <p>You can send the request through jquery or Fiddler (or any way you want). I tested it with Fiddler. You may see <a href="http://www.devcurry.com/2013/03/debugging-your-aspnet-web-apis-with.html#.Uin8vcaUGlQ" rel="nofollow noreferrer">How to debug a Web API through Fiddler</a></p> <p>Once your controller is setup, then build your project and start it in debug mode. Start Fiddler and go to composer. </p> <p>There paste the URI of your controller <em>(from IE/Brower)</em> in address bar, and select <code>POST</code> as Http method. Your URI would be like:</p> <pre><code>http://localhost:60291/api/values/GetUserList </code></pre> <p>In Fiddler Composer -> Request Header specify </p> <pre><code>Content-Type: application/json </code></pre> <p>In Fiddler Composer -> Request Body, specify your JSON </p> <pre><code>{ "UserId": 1, "CsvListOfIds": "2,3,4,5" } </code></pre> <p><em>(You can also create your C# template class from json using <a href="http://json2csharp.com/" rel="nofollow noreferrer">http://json2csharp.com/</a>)</em></p> <p>Put a debug point on your method in the controller and then in Fiddler Hit <code>Execute</code>, you will see that your Debug point in Visual studio is Hit and data is populated in the parameter. <img src="https://i.stack.imgur.com/Vq8LA.png" alt="enter image description here"></p> <p>Later you can return the <code>List&lt;User&gt;</code>, and you will see the response in Fiddler. </p> <pre><code>[{"ID":1},{"ID":2}] </code></pre> <p>Temporarily I created a class <code>User</code> like:</p> <pre><code>public class User { public int ID { get; set; } } </code></pre> <p>Hope this helps. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload