Reputation: 4255
I'm trying to create a simple WebAPI in ASP.NET. I put it in IIS. When I try to browse the site it's all good:
But when I try to get a result from the API, I get this error:
Controller:
public class ProductController : ApiController
{
Product[] products = new Product[]
{
new Product { Id = 1, Name = "Tomato Soup", Category = "Groceries", Price = 1 },
new Product { Id = 2, Name = "Yo-yo", Category = "Toys", Price = 3.75M },
new Product { Id = 3, Name = "Hammer", Category = "Hardware", Price = 16.99M }
};
public IEnumerable<Product> GetAllProducts()
{
return products;
}
public IHttpActionResult GetProduct(int id)
{
var product = products.FirstOrDefault((p) => p.Id == id);
if (product == null)
{
return NotFound();
}
return Ok(product);
}
}
WebApiConfig:
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
Upvotes: 2
Views: 7779
Reputation: 151720
You host it on the application accessed by /api
, so you need an additional /api
to match the routing:
http://localhost:6060/api/api/Product
If you don't want that, then either give the api
site a more sensible name, remove the api/
from the route, or both.
Upvotes: 2