【发布时间】:2018-01-12 18:38:57
【问题描述】:
我正在学习WebAP,之前做过Webservices。
我对 MVCWebApi 的实现感到困惑。
我的理解-它是基于HTTP协议的,所以它支持PUT、GET、POST、DELETE方法。
在某些教程中,我看到控制器中添加了 [HttpPost] 属性,在某些情况下,此属性未添加但仍然可以正常工作。 这可能是一个小问题,但会很清楚我的概念。请帮忙。
示例-下面将通过http://localhost:60486/api/products 以 json 格式提供有关员工的所有详细信息,即使没有 HttpGet 或 HttpPost 属性。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using WebApiTest.Models;
namespace WebApiTest.Controllers
{
public class ProductsController : 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 Product GetProduct(int id)
{
var product = products.FirstOrDefault((p) => p.Id == id);
return product;
}
}
}
【问题讨论】:
标签: asp.net-web-api