[英]WebAPI deployed to IIS, gives 404 Not Found
I'm trying to create a simple WebAPI in ASP.NET. 我正在尝试在ASP.NET中创建一个简单的WebAPI。 I put it in IIS. 我把它放在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: 但是,当我尝试从API获取结果时,出现以下错误:
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: 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 }
);
}
You host it on the application accessed by /api
, so you need an additional /api
to match the routing: 您将其托管在/api
访问的应用程序上,因此需要一个附加的/api
来匹配路由:
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. 如果您不希望这样做,则可以给api
网站一个更合理的名称,或者从路由中删除api/
,或者两者都选。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.