WebAPI deployed to IIS, gives 404 Not Found
我正在尝试在ASP.NET中创建一个简单的WebAPI。我把它放在IIS中。当我尝试浏览该站点时,一切都很好:
但是当我尝试从API获取结果时,出现此错误:
控制器:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 | 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 } ); } |
您将其托管在
1 | http://localhost:6060/api/api/Product |
如果您不希望这样做,请给