Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>A web api endpoint (controller) is a single resource that accepts get/post/put/delete verbs. It is <strong>not</strong> a normal MVC controller.</p> <p>Necessarily, at <code>/api/VTRouting</code> there can only be <strong>one</strong> HttpPost method that accepts the parameters you are sending. The function name <em>does not matter</em>, as long as you are decorating with the [http] stuff. I've never tried, though.</p> <p><strong>Edit:</strong> This does not work. In resolving, it seems to go by the number of parameters, not trying to model-bind to the type. </p> <p><em>You can overload the functions to accept different parameters. I am pretty sure you would be OK if you declared it the way you do, but used different (incompatible) parameters to the methods. If the params are the same, you are out of luck as model binding won't know which one you meant.</em> </p> <pre><code>[HttpPost] public MyResult Route(MyRequestTemplate routingRequestTemplate) {...} [HttpPost] public MyResult TSPRoute(MyOtherTemplate routingRequestTemplate) {...} </code></pre> <p><strong>This part works</strong></p> <p>The default template they give when you create a new one makes this pretty explicit, and I would say you should stick with this convention:</p> <pre><code>public class ValuesController : ApiController { // GET is overloaded here. one method takes a param, the other not. // GET api/values public IEnumerable&lt;string&gt; Get() { .. return new string[] ... } // GET api/values/5 public string Get(int id) { return "hi there"; } // POST api/values (OVERLOADED) public void Post(string value) { ... } public void Post(string value, string anotherValue) { ... } // PUT api/values/5 public void Put(int id, string value) {} // DELETE api/values/5 public void Delete(int id) {} } </code></pre> <p>If you want to make one class that does many things, for ajax use, there is no big reason to not use a standard controller/action pattern. The only real difference is your method signatures aren't as pretty, and you have to wrap things in <code>Json( returnValue)</code> before you return them.</p> <p><strong>Edit:</strong></p> <p>Overloading works just fine when using the standard template (edited to include) when using simple types. I've gone and tested the other way too, with 2 custom objects with different signatures. Never could get it to work.</p> <ul> <li>Binding with complex objects doesn't look "deep", so thats a no-go</li> <li>You could get around this by passing an extra param, on the query string</li> <li><a href="http://www.west-wind.com/weblog/posts/2012/May/08/Passing-multiple-POST-parameters-to-Web-API-Controller-Methods">A better writeup than I can give</a> on available options</li> </ul> <p>This worked for me in this case, see where it gets you. Exception for testing only.</p> <pre><code>public class NerdyController : ApiController { public void Post(string type, Obj o) { throw new Exception("Type=" + type + ", o.Name=" + o.Name ); } } public class Obj { public string Name { get; set; } public string Age { get; set; } } </code></pre> <p>And called like this form the console:</p> <pre><code>$.post("/api/Nerdy?type=white", { 'Name':'Slim', 'Age':'21' } ) </code></pre>
 

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