.net Swagger模型模式为空 - 仅适用于UNION形成的GET

时间:2017-08-07 12:39:48

标签: c# .net swagger swashbuckle

.net Swagger模型模式仅对UNION形成的GET为空(简单的GET显示正确的模型模式)

我怎样才能为方法显示一个好的模型架构swagger显示一个空架构?

EMPTY架构: enter image description here

GOOD架构: enter image description here

2 个答案:

答案 0 :(得分:1)

像这样使用 SwaggerResponse

<?php

    $sql = "SELECT * FROM image"; 
    $result = mysqli_query($conn, $sql);
    $getResult = mysqli_fetch_assoc($result);

    $numberOfResults = mysqli_num_rows($result);
    $resultsPerPage = 5;
    $numberOfPages = ceil($numberOfResults/$resultsPerPage);

    if (!isset($_GET['page'])) {
        $page = 1;
    } else {
        $page = $_GET['page'];
    }

    $currentPageResults = ($page-1)*$resultsPerPage;

    $sql2 = "SELECT * FROM image ORDER BY id DESC LIMIT ".$currentPageResults.','.$resultsPerPage;
    $result2 = mysqli_query($conn, $sql2);

    while($row = $result2->fetch_assoc()) { 
        echo    "<div class='imageContainer'>" 
                ."<h1>".$row["name"].'</h1>' 
                .'<div class="stickyImageContainer"><a href="imageInfo.php?image='.$row["path"].'"><img class="uploadedImg" src="uploads/'.$row["path"] .'" alt="Random image" /></a> 
                </div></div>';
        echo    "<form id='upvoteImage' method='POST' action='".upvoteImage($conn)."'>
                    <input type='hidden' name='id' value='".$row['id']."'>
                    <button id='upvoteImage' type='submit' name='upvoteImage'>Upvote</button>
                </form>";
        echo $row['id'];

        echo    "<form id='downvoteImage' method='POST' action=''>
                    <input type='hidden' name='id' value='".$row['id']."'>
                    <button id='downvoteImage' type='submit' name='downvoteImage'>Downvote</button>
                </form>";  
    }


    echo '<div class="imageContainer">';
    for ($page=1; $page<=$numberOfPages; $page++) {
        echo '<div class="pagination"><a href="index.php?page='.$page.'"><p id="paginationP">'.$page.'</p></a></div>';
    }
    echo '</div>';
?>



你们之间的区别在于人们使用HttpResponseMessage

enter image description here

enter image description here

答案 1 :(得分:0)

部分问题是由@HelderSepu观察到的,我发送的响应不是IEnumerable(因为&#34; Select&#34;在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>))]