C#,Web API,Swashbuckler/Swagger [英] c#, web api, swashbuckler/swagger

查看:138
本文介绍了C#,Web API,Swashbuckler/Swagger的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的第一个Web API,第一个Web应用程序,也是Ive第一次通过swashbuckler使用swagger. 我正在为设备数据库表使用TPH.
例如,我有一个名为equipment的基类,其中几个子类是从equipment继承的. (这不是真正的代码,因此我并没有在所有公众场合露面)
正如我所期望的那样,当我在浏览器中运行swagger时,模型描述只是返回设备类的模型
我已经在Google上搜索了如何显示所有可能的模型,但是由于它们与c#或使用注释文档的api控制器无关,因此无法理解其答复,或者它们是为那些比我理解得更多的人写的. br/> 任何人都可以发表示例,或者请给我指向初学者教程,以获取关于如何迅速显示所有可能的模型的信息.我不得不写几个不同的api调用来解决这个问题,只是显示了模型.
谢谢

This is my first web api, first web app, and first time Ive used swagger via swashbuckler. I'm using TPH for my equipment db table.
For example, I have base class called equipment with several child classes inherited from equipment. (This isnt real code so Ive not put in all the publics etc)
As I sorta expect, the model description when I run swagger in my browser, just returns the model for the equipment class
I've googled how to show all possible models but can't understand the replies as they don't relate to c#, or api controllers using comments for documentation, or they're written for people who understand this more than I do.
is anybody able to post an example, or point me to a beginner tutorial please, on how to get swagger display all possible models. I've had to write several different api calls to get around this, just so the models are shown.
thanks

class equipment
{
  public int id{get;set;}
  public int foo{get;set;}
  public int bar{get;set;}
}

class bucket : equipment
{      
  public int booboo{get;set;}
}
class mop : equipment
{
  public int lala{get;set;}
}


// equipmentcontroller class get function

/// <summary>
/// Returns a piece of equipment.<br/>
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[Route(@"{id:int}"), ResponseType(typeof(equipment))]
public IHttpActionResult GetMop(int id)
{
  return result != null ? (IHttpActionResult)Ok(GetMop[id]) : NotFound();
}

推荐答案

您的建议是不可能的.在Swagger中,操作响应的定义基本上是不同HTTP状态代码到特定响应对象的映射. (请查看 http://swagger.io/specification/#responsesObject )

What you're suggesting isn't possible. In Swagger, the definition of responses for an operation is basically a mapping of different HTTP status codes to specific response objects. (Have a look at http://swagger.io/specification/#responsesObject)

您不可能说一个操作的200 OK可能返回代表拖把的响应或代表桶的响应.

It's not going to be possible for you to represent that a 200 OK for an operation could either return a response representing a mop or a response representing a bucket.

您提到我不得不编写几个不同的api调用来解决这个问题,只是显示了模型.",这就是我期望的RESTful API.不同类型的资源由不同的终结点处理.

You mention "I've had to write several different api calls to get around this, just so the models are shown.", this is what I'd expect from a RESTful API. Different kinds of resources are handled by different endpoints.

这篇关于C#,Web API,Swashbuckler/Swagger的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆