2017-08-07 33 views

回答

1

使用SwaggerResponse像這樣的:

[SwaggerResponse(HttpStatusCode.OK, "List of customers", typeof(IEnumerable<int>))] 
    [SwaggerResponse(HttpStatusCode.BadRequest, Type = typeof(BadRequestErrorMessageResult))] 
    [SwaggerResponse(HttpStatusCode.NotFound, Type = typeof(NotFoundResult))] 
    public IHttpActionResult GetById(int id) 
    { 
     if (id > 0) 
      return Ok(new int[] { 1, 2 }); 
     else if (id == 0) 
      return NotFound(); 
     else 
      return BadRequest("id must be greater than 0"); 
    } 



你之間的區別是一個使用HttpResponseMessage

enter image description here

enter image description here

0

問題的部分是由@HelderSepu觀察,我發出的響應不是IEnumerable的(因爲Linq中的 「選擇」)。

  1. 所以我需要創建一個模型類,纔能有正確的模型架構:

    namespace SupplierB_api.Models { 
        public class SupplierResponse { 
        public int SupplierID; 
        public string SupplierName; 
        public char SupplierType; } 
    } 
    
  2. 然後,我用的是模型的Linq:

      var SuppliersDistributor = entities.tblD.Take(5).AsEnumerable() 
           .Select(d => new SupplierResponse { SupplierID = d.distributor_id, SupplierName = d.distributor_name, SupplierType = 'D'}); 
    
          var SuppliersPublisher = entities.tblN.Take(5).AsEnumerable() 
           .Select(p => new SupplierResponse { SupplierID = p.publisher_id, SupplierName = p.publisher_name, SupplierType = 'P' }); 
    
  3. 然後,我使用SwaggerResponse(由@HelderSepu所建議的):

    [SwaggerResponse(HttpStatusCode.OK, "response", typeof(IOrderedEnumerable<SupplierResponse>))]