【发布时间】:2018-12-04 22:14:46
【问题描述】:
我正在尝试创建一个 API 并尝试通过 chrome 访问它,期望它返回项目列表
public class ProductController : ApiController
{
Product product = new Product();
List<Product> productList = new List<Product>();
[HttpGet]
public HttpResponseMessage GetTheProduct(int id)
{
this.productList.Add(new Product {Id = 111,Name= "sandeep" });
return Request.CreateResponse(HttpStatusCode.OK, this.productList.FirstOrDefault(p => p.Id == 111));
}
}
我还没有添加路由,所以想使用默认路由运行它,但是当我运行它时,我得到了
没有找到与请求匹配的 HTTP 资源 URI 'http://localhost:65098/api/GetTheProduct()'。 找不到与名为的控制器匹配的类型 'GetTheProduct()'。
建议我需要什么才能让它工作。
【问题讨论】:
-
例如尝试使用 .../api/product/gettheproduct/11 吗?
-
是的,我尝试使用 localhost:65098/api/product/gettheproduct/111 仍然出错
-
发布您的 WebAPIConfig.cs 代码可能有助于找到问题。
标签: c# asp.net-web-api asp.net-web-api-routing