需要帮助调用 Web Api 控制器方法来检索数据
Need help calling Web Api controller methods to retrieve data
我是 Web 新手 Api(我可能在这里遗漏了一些非常简单的东西)我有一个 Web Api 项目 ProductsController.cs
有一个 属性 类型 List<Product>
我只是想在浏览器中调用 Api 例如 localhost/api/products/1
或 /api/products/getproduct/1
以检索 [=28= 中指定 ID 的产品响应] 但我无法获取它来检索任何数据。我每次都会收到 'not found' 错误。我缺少什么让它找到数据并检索响应?
我试过以下方法:
public IHttpActionResult Get(int id)
{
var product = products.FirstOrDefault(p => p.Id == id);
if (product == null)
{
return NotFound();
}
else
{
return Ok(product);
}
}
甚至还 returns 未找到以下内容:
public string Get(int id)
{
return "product test";
}
确保路由配置正确
WebApiConfig.cs
public static class WebApiConfig {
public static void Register(HttpConfiguration config) {
// Attribute routing.
config.MapHttpAttributeRoutes();
// Convention-based routing.
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
从那里你有两个路由到动作的选项。
基于约定。
public class ProductsController : ApiController {
//...constructor code removed for brevity
[HttpGet] // Matches GET api/products
public IHttpActionResult GetAllProducts() {
return Ok(products);
}
[HttpGet] // Matches GET api/products/1
public IHttpActionResult GetProduct(int id) {
var product = products.FirstOrDefault(p => p.Id == id);
if (product == null) {
return NotFound();
}
return Ok(product);
}
}
或属性路由
[RoutePrefix("api/products")]
public class ProductsController : ApiController {
//...constructor code removed for brevity
[HttpGet]
[Route("")] // Matches GET api/products
public IHttpActionResult GetAllProducts() {
return Ok(products);
}
[HttpGet]
[Route("{id:int}")] // Matches GET api/products/1
public IHttpActionResult GetProduct(int id) {
var product = products.FirstOrDefault(p => p.Id == id);
if (product == null) {
return NotFound();
}
return Ok(product);
}
}
我是 Web 新手 Api(我可能在这里遗漏了一些非常简单的东西)我有一个 Web Api 项目 ProductsController.cs
有一个 属性 类型 List<Product>
我只是想在浏览器中调用 Api 例如 localhost/api/products/1
或 /api/products/getproduct/1
以检索 [=28= 中指定 ID 的产品响应] 但我无法获取它来检索任何数据。我每次都会收到 'not found' 错误。我缺少什么让它找到数据并检索响应?
我试过以下方法:
public IHttpActionResult Get(int id)
{
var product = products.FirstOrDefault(p => p.Id == id);
if (product == null)
{
return NotFound();
}
else
{
return Ok(product);
}
}
甚至还 returns 未找到以下内容:
public string Get(int id)
{
return "product test";
}
确保路由配置正确
WebApiConfig.cs
public static class WebApiConfig {
public static void Register(HttpConfiguration config) {
// Attribute routing.
config.MapHttpAttributeRoutes();
// Convention-based routing.
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
从那里你有两个路由到动作的选项。
基于约定。
public class ProductsController : ApiController {
//...constructor code removed for brevity
[HttpGet] // Matches GET api/products
public IHttpActionResult GetAllProducts() {
return Ok(products);
}
[HttpGet] // Matches GET api/products/1
public IHttpActionResult GetProduct(int id) {
var product = products.FirstOrDefault(p => p.Id == id);
if (product == null) {
return NotFound();
}
return Ok(product);
}
}
或属性路由
[RoutePrefix("api/products")]
public class ProductsController : ApiController {
//...constructor code removed for brevity
[HttpGet]
[Route("")] // Matches GET api/products
public IHttpActionResult GetAllProducts() {
return Ok(products);
}
[HttpGet]
[Route("{id:int}")] // Matches GET api/products/1
public IHttpActionResult GetProduct(int id) {
var product = products.FirstOrDefault(p => p.Id == id);
if (product == null) {
return NotFound();
}
return Ok(product);
}
}