Ich versuche, eine einfache WebAPI in ASP.NET zu erstellen. Ich habe es in IIS eingefügt. Wenn ich versuche, die Seite zu durchsuchen, es ist alles gut:WebAPI in IIS bereitgestellt, gibt 404 nicht gefunden
Aber wenn ich versuche, ein Ergebnis aus der API zu bekommen, bekomme ich diesen Fehler:
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 }
);
}
Ich bekomme immer noch den gleichen Fehler – Pachu
Das ist, weil Ihr Controller ProduktController heißt, Singular, so dass Sie Produkt, nicht Produkte benötigen. – CodeCaster
Ich habe es versucht, aber immer noch, ich bekomme das gleiche Ergebnis – Pachu