【问题标题】:WebApi implementationWebApi 实现
【发布时间】: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


    【解决方案1】:

    编辑2

    attribute-routing-in-web-api-2

    HTTP 方法 Web API 还根据 HTTP 方法选择操作 请求(GET、POST 等)。默认情况下,Web API 会查找 与控制器方法名称的开头匹配不区分大小写。 例如,名为 PutCustomers 的控制器方法匹配 HTTP PUT 请求。

    您可以通过使用任何 以下属性:2

    [HttpDelete] [HttpGet] [HttpHead] [HttpOptions] [HttpPatch] [HttpPost] [HttpPut]

    默认情况下,如果动作方法名称以 HttpAttribute 开头 示例:

    public IHttpActionResult GetValue(int id)
    {
        return Ok();
    }
    

    api知道这个动作是Get,所以不用加[HttpGet]

    但是如果你想拥有不同的动作名称,那么你必须添加 [HttpAttribute]

    例子

    [HttpGet]
    public IHttpActionResult Value(int id)
    {
        return Ok();
    }
    

    编辑

    基于您的示例:

    //This will work, because name GetAllProducts starts with Get, so api knows 
    ///its HttpGet, You don't have to use [HttpGet]Attribute  (but You can add 
    //it, and it will work as well
    public IEnumerable<Product> GetAllProducts()
    {
        return products;
    }
    
    //This will work, because name GetProduct starts with Get, so api knows 
    ///its HttpGet, You don't have to use [HttpGet]Attribute  (but You can add 
    //it, and it will work as well
    public Product GetProduct(int id)
    {
        var product = products.FirstOrDefault((p) => p.Id == id);
    
        return product;
    }
    

    但如果您将名称更改为:

    //This will NOT work, because name AllProducts dont starts with Get, so api dont know what http method to use
    ///You have to add [HttpGet]
    public IEnumerable<Product> AllProducts()
    {
        return products;
    }
    

    所以正确的实现(更改名称后)是这样的:

    [HttpGet]
    public IEnumerable<Product> AllProducts()
    {
        return products;
    }
    

    最后一件事,您可以使用属性更改默认的 http 映射:

    //This action will response to POST, not get
    [HttpPost]
    public IEnumerable<Product> GetAllProducts()
    {
        return products;
    }
    

    【讨论】:

    • 谢谢,但在我的示例代码中,我没有使用任何这些,但它仍然以 json 形式给我产品信息。你在谈论添加“Get”前缀吗?如果我使用“get”[小写字母),我不能在没有get属性的情况下使用它吗?
    • action 的命名约定是以大写字母开头的方法名。
    • 如此基本但又如此重要。非常感谢:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-25
    • 2013-09-05
    • 1970-01-01
    • 1970-01-01
    • 2014-04-17
    • 2021-07-26
    • 1970-01-01
    相关资源
    最近更新 更多