Golan Kiviti
Golan Kiviti

Reputation: 4255

WebAPI deployed to IIS, gives 404 Not Found

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:

enter image description here

But when I try to get a result from the API, I get this error:

enter image description here

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

Answers (1)

CodeCaster
CodeCaster

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

Related Questions