Asp.net web api ASP.NET WebApi操作返回类型

Asp.net web api ASP.NET WebApi操作返回类型,asp.net-web-api,Asp.net Web Api,我的行动应该是什么 键入的对象/集合:Customer或IEnumerable HttpResponseMessage IHttpActionResult 返回后两个中的一个的最佳方式是什么?我应该使用Ok还是Contenthelper方法?框架将所有操作结果转换为HttpResponseMessage,因此列出的三个操作的结果将是相同的。然而,最后一种方法是向客户返回结果的首选方法,因为它更简洁。让我们看一看三个正在运行的选项 假设有这样一个产品存储库: public interface IP

我的行动应该是什么

  • 键入的对象/集合:
    Customer
    IEnumerable
  • HttpResponseMessage
  • IHttpActionResult

  • 返回后两个中的一个的最佳方式是什么?我应该使用
    Ok
    还是
    Content
    helper方法?

    框架将所有操作结果转换为HttpResponseMessage,因此列出的三个操作的结果将是相同的。然而,最后一种方法是向客户返回结果的首选方法,因为它更简洁。让我们看一看三个正在运行的选项

    假设有这样一个产品存储库:

    public interface IProductRepository
        {
            Product FindProductById(int productId);
        }
    
        public class ProductRepository : IProductRepository
        {
            private List<Product> products = new List<Product>();
            private int _nextId = 1;
    
            public ProductRepository()
            {
                Add(new Product { Name = "Tomato soup", Category = "Groceries", Price = 1.39M });
                Add(new Product { Name = "Yo-yo", Category = "Toys", Price = 3.75M });
                Add(new Product { Name = "Hammer", Category = "Hardware", Price = 16.99M });
            }
    
            public IEnumerable<Product> GetAll()
            {
                return products;
            }
    
            public Product FindProductById(int id)
            {
                return products.Find(p => p.Id == id);
            }
    
            public Product Add(Product item)
            {
                if (item == null)
                {
                    throw new ArgumentNullException("item");
                }
                item.Id = _nextId++;
                products.Add(item);
                return item;
            }
    
        }
    
    现在,添加一个get操作,该操作将对象作为结果返回

            [Route("products/{productId}")]
            public Product GetReturnsObjects(int productId)
            {
                Product product = _repository.FindProductById(productId);
    
                if(product ==null)   //Throwing the exception skips the entire Http Response pipeline
                    throw new HttpResponseException(HttpStatusCode.NotFound);
    
                return product;
            }
    
    如您所见,为了返回除产品之外的任何内容,我们必须抛出一个带有适当HttpStatusCode的异常。这样做将跳过响应管道,除非HttpConfiguration中存在异常筛选器

    接下来,让我们创建返回HttpResponseMessage或IHttpActionResult的GET方法

            [Route("messageproducts/{productId}")]
            public HttpResponseMessage GetReturnResponseMessage(int productId)
            {
                 Product product = _repository.FindProductById(productId);
    
                if(product ==null)
                    return new HttpResponseMessage(HttpStatusCode.NotFound);
    
                return Request.CreateResponse(HttpStatusCode.OK, product);
            }
    
             [Route("actionresultproducts/{productId}")]
            public IHttpActionResult GetReturnsActionResult(int productId)
            {
                  Product product = _repository.FindProductById(productId);
    
                if (product == null)
                    return NotFound();
    
    
                return Ok(product);   //Same as Content(HttpStatusCode.OK, product);
            }
    
    显然,后者更简短

    关于Ok()或Content(),出于同样的原因,您应该使用Ok()


    希望这能有所帮助。

    我偶然发现了这个问题,因为我有同样的问题+1.举例说明。你知道微软(再次)提供多种方法的原因吗?根据你的解释,“退货产品”这类东西本来就不应该存在,但在他们的例子中,这似乎是微软的首选方式。哦,我喜欢IHttpActionResult,像“return NotFound()”这样的东西很好地借鉴了大多数产品中无处不在的语言。@Acrotygma我想他们只是想让API方法看起来像普通的.Net方法,所以这就是设计“return Object”的原因。此外,您看到的示例可能来自API的版本1,该版本没有IHttpActionResult。
            [Route("messageproducts/{productId}")]
            public HttpResponseMessage GetReturnResponseMessage(int productId)
            {
                 Product product = _repository.FindProductById(productId);
    
                if(product ==null)
                    return new HttpResponseMessage(HttpStatusCode.NotFound);
    
                return Request.CreateResponse(HttpStatusCode.OK, product);
            }
    
             [Route("actionresultproducts/{productId}")]
            public IHttpActionResult GetReturnsActionResult(int productId)
            {
                  Product product = _repository.FindProductById(productId);
    
                if (product == null)
                    return NotFound();
    
    
                return Ok(product);   //Same as Content(HttpStatusCode.OK, product);
            }