How many number of GETs, POSTs allowed ?http://forums.asp.net/t/1770954.aspx/1?How+many+number+of+GETs+POSTs+allowed+Tue, 21 Feb 2012 04:38:10 -050017709544838440http://forums.asp.net/p/1770954/4838440.aspx/1?How+many+number+of+GETs+POSTs+allowed+How many number of GETs, POSTs allowed ? <p>Can you only have one GET method ?&nbsp;&nbsp; I tried with 2 GET methods:</p> <pre class="prettyprint">public Product GetProductById(int id) public Product GetProductByName(string name)</pre> <pre class="prettyprint">// And I get: <br /><br />http://localhost:4833/api/products/undefined "The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'HelloWebAPI.Models.Product GetProductById(Int32)'<br /><br /><br />// I added a new route with the {name} token, but it seems that it doesn't get the route.<br /><br /><br /></pre> 2012-02-18T04:44:35-05:004838564http://forums.asp.net/p/1770954/4838564.aspx/1?Re+How+many+number+of+GETs+POSTs+allowed+Re: How many number of GETs, POSTs allowed ? <p></p> <blockquote><span class="icon-blockquote"></span> <h4>zebula8</h4> http://localhost:4833/api/products/undefined</blockquote> <p></p> <p>this</p> <p>undefined</p> <p>does not correspond wuth </p> <blockquote><span class="icon-blockquote"></span> <h4>zebula8</h4> GetProductById(int id)</blockquote> or&nbsp; <blockquote><span class="icon-blockquote"></span> <h4>zebula8</h4> GetProductByName(string name)</blockquote> <p></p> <p>Please see http://bit.ly/mvc_ajax_jquery</p> 2012-02-18T07:28:19-05:004838650http://forums.asp.net/p/1770954/4838650.aspx/1?Re+How+many+number+of+GETs+POSTs+allowed+Re: How many number of GETs, POSTs allowed ? <p>I agree that designing URLs in this fashion isn't good, but doing this is entirely possible with some route magic and constraints.<br> The first route rule catches URLs with strings that match the regex which looks for numbers.&nbsp; The second rule takes effect if the first one doesn't.</p> <pre class="prettyprint">routes.MapHttpRoute( name: &quot;ProductsById&quot;, routeTemplate: &quot;products/{id}&quot;, defaults: new { controller = &quot;Products&quot;, action = &quot;GetProductById&quot; }, constraints: new { id = @&quot;\d&#43;&quot; } ); routes.MapHttpRoute( name: &quot;ProductsByName&quot;, routeTemplate: &quot;products/{name}&quot;, defaults: new { controller = &quot;Products&quot;, action = &quot;GetProductByName&quot; } );</pre> 2012-02-18T09:50:12-05:004839123http://forums.asp.net/p/1770954/4839123.aspx/1?Re+How+many+number+of+GETs+POSTs+allowed+Re: How many number of GETs, POSTs allowed ? <p>Here's the routes in my global:</p> <pre class="prettyprint">routes.MapHttpRoute( name: &quot;ProductByName&quot;, routeTemplate: &quot;api/products/{name}&quot;, defaults: new { controller = &quot;products&quot;, action = &quot;GetProductByName&quot; } ); routes.MapHttpRoute( name: &quot;ProductById&quot;, routeTemplate: &quot;api/products/{id}&quot;, defaults: new { controller = &quot;products&quot;, action = &quot;GetProductById&quot; }, constraints: new { id = @&quot;\d&#43;&quot; } ); routes.MapHttpRoute( name: &quot;DefaultApi&quot;, routeTemplate: &quot;api/{controller}/{id}&quot;, defaults: new { id = RouteParameter.Optional } );</pre> <p></p> <p>All i get now is resouce not found.</p> <p></p> <p>Here is the controller code:</p> <p></p> <pre class="prettyprint"> public class ProductsController : ApiController { public IEnumerable&lt;Product&gt; GetAllProducts() { return new List&lt;Product&gt; { new Product() {Id = 1, Name = "Gizmo 1", Price = 1.99M}, new Product() {Id = 2, Name = "Gizmo 2", Price = 2.99M}, new Product() {Id = 3, Name = "Gizmo 3", Price = 3.99M} }; } public Product GetProductById(int id) { if (id &lt; 1 || id &gt; 3) throw new HttpResponseException(System.Net.HttpStatusCode.NotFound); return new Product() { Id = id, Name = "Gizmo" + id, Price = id + 0.99M}; } public Product GetProductByName(string name) { return new Product() { Id = 1, Name = name + "1", Price = 1.99M }; } }</pre> 2012-02-18T22:35:21-05:004839300http://forums.asp.net/p/1770954/4839300.aspx/1?Re+How+many+number+of+GETs+POSTs+allowed+Re: How many number of GETs, POSTs allowed ? <p>You need to have the ProductById rule at top, the order does matter.&nbsp; Otherwise the name one will catch it all.</p> <p>I did try the code before submitting my suggestion, and it worked just fine :)&nbsp; Let me know if you cant get it to work after moving the rules around.</p> 2012-02-19T03:19:37-05:004839334http://forums.asp.net/p/1770954/4839334.aspx/1?Re+How+many+number+of+GETs+POSTs+allowed+Re: How many number of GETs, POSTs allowed ? <p>I moved it to the top but still get:&nbsp;</p> <h2><i>The resource cannot be found.</i></h2> 2012-02-19T04:16:12-05:004839594http://forums.asp.net/p/1770954/4839594.aspx/1?Re+How+many+number+of+GETs+POSTs+allowed+Re: How many number of GETs, POSTs allowed ? <p>What URLs are you using when you get the 404?</p> 2012-02-19T10:57:19-05:004839924http://forums.asp.net/p/1770954/4839924.aspx/1?Re+How+many+number+of+GETs+POSTs+allowed+Re: How many number of GETs, POSTs allowed ? <p>[http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api]</p> <p>That's the link where I tried this sample and decided if I could use more than one GET.</p> <p>I'm using the jquery ajax exactly as shown in this sample but with one additional one for the name:</p> <p></p> <pre class="prettyprint">&lt;script type=&quot;text/javascript&quot;&gt; $(document).ready(function () { // for GetAllProducts $.getJSON(&quot;api/products/&quot;, function (data) { // on success, 'data' contains a list of products $.each(data, function (key, val) { // format the text to display var str = val.Name &#43; ': $ ' &#43; val.Price; // add a list item for the product $('&lt;li&gt;', { html: str }).appendTo($('#products')); }); }); // for GetProductById $('#find').click(function () { var id = $('#prodid').val(); $.getJSON('api/products/' &#43; id, function (data) { var str = data.Name &#43; ': $' &#43; data.Price; $('#product').html(str); }) .fail( function (jqXHR, textStatus, error) { $('#product').html('Error: ' &#43; error); }); }); // for GetProductByName() $('#findname').click(function () { var name = $('#nameid').val(); $.getJSON('api/products/' &#43; name, function (data) { var str = data.Name &#43; ': $' &#43; data.Price; $('#product').html(str); }) .fail( function (jqXHR, textStatus, error) { $('#product').html('Error: ' &#43; error); }); }); }); &lt;/script&gt;</pre> <p></p> 2012-02-19T19:42:49-05:004840689http://forums.asp.net/p/1770954/4840689.aspx/1?Re+How+many+number+of+GETs+POSTs+allowed+Re: How many number of GETs, POSTs allowed ? <p>Did you try entering the URLs directly into a browser to rule out errors in the Javascript code?</p> 2012-02-20T08:31:51-05:004842158http://forums.asp.net/p/1770954/4842158.aspx/1?Re+How+many+number+of+GETs+POSTs+allowed+Re: How many number of GETs, POSTs allowed ? <p>I am able to hit the following urls from the browser by just copy pasting the routes and controller mentioned above.</p> <p><a href="http://localhost:56554/api/products">http://localhost:56554/api/products</a></p> <p><a href="http://localhost:56554/api/products/2">http://localhost:56554/api/products/2</a></p> <p><a href="http://localhost:56554/api/products/blah">http://localhost:56554/api/products/blah</a></p> <p>I see that in the GetProductById action, you are explicitly returning a 'NotFound' status code if the id &lt; 1 or id &gt; 3. Are you sure you are not using an id outside this range which could be causing the 'Not Found' ?</p> 2012-02-21T04:38:10-05:00