Can you only have one GET method ? I tried with 2 GET methods:
public Product GetProductById(int id)
public Product GetProductByName(string name)
// And I get:
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)'
// I added a new route with the {name} token, but it seems that it doesn't get the route.
I agree that designing URLs in this fashion isn't good, but doing this is entirely possible with some route magic and constraints.
The first route rule catches URLs with strings that match the regex which looks for numbers. The second rule takes effect if the first one doesn't.
routes.MapHttpRoute(
name: "ProductsById",
routeTemplate: "products/{id}",
defaults: new { controller = "Products", action = "GetProductById" },
constraints: new { id = @"\d+" }
);
routes.MapHttpRoute(
name: "ProductsByName",
routeTemplate: "products/{name}",
defaults: new { controller = "Products", action = "GetProductByName" }
);
routes.MapHttpRoute(
name: "ProductByName",
routeTemplate: "api/products/{name}",
defaults: new { controller = "products", action = "GetProductByName" }
);
routes.MapHttpRoute(
name: "ProductById",
routeTemplate: "api/products/{id}",
defaults: new { controller = "products", action = "GetProductById" },
constraints: new { id = @"\d+" }
);
routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
All i get now is resouce not found.
Here is the controller code:
public class ProductsController : ApiController
{
public IEnumerable<Product> GetAllProducts()
{
return new List<Product>
{
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 < 1 || id > 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 };
}
}
That's the link where I tried this sample and decided if I could use more than one GET.
I'm using the jquery ajax exactly as shown in this sample but with one additional one for the name:
<script type="text/javascript">
$(document).ready(function () {
// for GetAllProducts
$.getJSON("api/products/", function (data) {
// on success, 'data' contains a list of products
$.each(data, function (key, val) {
// format the text to display
var str = val.Name + ': $ ' + val.Price;
// add a list item for the product
$('<li>', { html: str }).appendTo($('#products'));
});
});
// for GetProductById
$('#find').click(function () {
var id = $('#prodid').val();
$.getJSON('api/products/' + id, function (data) {
var str = data.Name + ': $' + data.Price;
$('#product').html(str);
})
.fail(
function (jqXHR, textStatus, error) {
$('#product').html('Error: ' + error);
});
});
// for GetProductByName()
$('#findname').click(function () {
var name = $('#nameid').val();
$.getJSON('api/products/' + name, function (data) {
var str = data.Name + ': $' + data.Price;
$('#product').html(str);
})
.fail(
function (jqXHR, textStatus, error) {
$('#product').html('Error: ' + error);
});
});
});
</script>
I see that in the GetProductById action, you are explicitly returning a 'NotFound' status code if the id < 1 or id > 3. Are you sure you are not using an id outside this range which could be causing the 'Not Found' ?
zebula8
Member
9 Points
7 Posts
How many number of GETs, POSTs allowed ?
Feb 18, 2012 04:44 AM|LINK
Can you only have one GET method ? I tried with 2 GET methods:
ignatandrei
All-Star
134960 Points
21632 Posts
Moderator
MVP
Re: How many number of GETs, POSTs allowed ?
Feb 18, 2012 07:28 AM|LINK
this
undefined
does not correspond wuth
orPlease see http://bit.ly/mvc_ajax_jquery
SiggiGG
Member
265 Points
105 Posts
Re: How many number of GETs, POSTs allowed ?
Feb 18, 2012 09:50 AM|LINK
I agree that designing URLs in this fashion isn't good, but doing this is entirely possible with some route magic and constraints.
The first route rule catches URLs with strings that match the regex which looks for numbers. The second rule takes effect if the first one doesn't.
routes.MapHttpRoute( name: "ProductsById", routeTemplate: "products/{id}", defaults: new { controller = "Products", action = "GetProductById" }, constraints: new { id = @"\d+" } ); routes.MapHttpRoute( name: "ProductsByName", routeTemplate: "products/{name}", defaults: new { controller = "Products", action = "GetProductByName" } );zebula8
Member
9 Points
7 Posts
Re: How many number of GETs, POSTs allowed ?
Feb 18, 2012 10:35 PM|LINK
Here's the routes in my global:
routes.MapHttpRoute( name: "ProductByName", routeTemplate: "api/products/{name}", defaults: new { controller = "products", action = "GetProductByName" } ); routes.MapHttpRoute( name: "ProductById", routeTemplate: "api/products/{id}", defaults: new { controller = "products", action = "GetProductById" }, constraints: new { id = @"\d+" } ); routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } );All i get now is resouce not found.
Here is the controller code:
public class ProductsController : ApiController { public IEnumerable<Product> GetAllProducts() { return new List<Product> { 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 < 1 || id > 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 }; } }SiggiGG
Member
265 Points
105 Posts
Re: How many number of GETs, POSTs allowed ?
Feb 19, 2012 03:19 AM|LINK
You need to have the ProductById rule at top, the order does matter. Otherwise the name one will catch it all.
I did try the code before submitting my suggestion, and it worked just fine :) Let me know if you cant get it to work after moving the rules around.
zebula8
Member
9 Points
7 Posts
Re: How many number of GETs, POSTs allowed ?
Feb 19, 2012 04:16 AM|LINK
I moved it to the top but still get:
The resource cannot be found.
SiggiGG
Member
265 Points
105 Posts
Re: How many number of GETs, POSTs allowed ?
Feb 19, 2012 10:57 AM|LINK
What URLs are you using when you get the 404?
zebula8
Member
9 Points
7 Posts
Re: How many number of GETs, POSTs allowed ?
Feb 19, 2012 07:42 PM|LINK
[http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api]
That's the link where I tried this sample and decided if I could use more than one GET.
I'm using the jquery ajax exactly as shown in this sample but with one additional one for the name:
<script type="text/javascript"> $(document).ready(function () { // for GetAllProducts $.getJSON("api/products/", function (data) { // on success, 'data' contains a list of products $.each(data, function (key, val) { // format the text to display var str = val.Name + ': $ ' + val.Price; // add a list item for the product $('<li>', { html: str }).appendTo($('#products')); }); }); // for GetProductById $('#find').click(function () { var id = $('#prodid').val(); $.getJSON('api/products/' + id, function (data) { var str = data.Name + ': $' + data.Price; $('#product').html(str); }) .fail( function (jqXHR, textStatus, error) { $('#product').html('Error: ' + error); }); }); // for GetProductByName() $('#findname').click(function () { var name = $('#nameid').val(); $.getJSON('api/products/' + name, function (data) { var str = data.Name + ': $' + data.Price; $('#product').html(str); }) .fail( function (jqXHR, textStatus, error) { $('#product').html('Error: ' + error); }); }); }); </script>SiggiGG
Member
265 Points
105 Posts
Re: How many number of GETs, POSTs allowed ?
Feb 20, 2012 08:31 AM|LINK
Did you try entering the URLs directly into a browser to rule out errors in the Javascript code?
vinelap
Member
28 Points
9 Posts
Microsoft
Re: How many number of GETs, POSTs allowed ?
Feb 21, 2012 04:38 AM|LINK
I am able to hit the following urls from the browser by just copy pasting the routes and controller mentioned above.
http://localhost:56554/api/products
http://localhost:56554/api/products/2
http://localhost:56554/api/products/blah
I see that in the GetProductById action, you are explicitly returning a 'NotFound' status code if the id < 1 or id > 3. Are you sure you are not using an id outside this range which could be causing the 'Not Found' ?