简体   繁体   English

Swashbuckle / swagger 文档缺失信息

[英]Swashbuckle / swagger Document missing information

I can't seem to understand why my swagger doc UI is missing the Model Schema details for each Gets and Posts within my controller?我似乎无法理解为什么我的 swagger doc UI 缺少​​控制器中每个 Gets 和 Posts 的模型架构详细信息?

I am running SwashBuckle for ASP.NET core nuget package v4.0.1 and even after upgrading to the latest package does nothing to show the schema details?我正在为 ASP.NET 核心 nuget 包 v4.0.1 运行SwashBuckle ,即使升级到最新包后也没有显示架构详细信息? (My WebAPI is build in Core 2.2) (我的 WebAPI 是在 Core 2.2 中构建的)

I have ran through the swagger documentation only wrt configurations and nothing directs me to where I can get the additional information to be displayed?我只浏览了 swagger 文档,但没有任何内容可以指导我在哪里可以获取要显示的附加信息?

After some research I found that if I use the following Attribute经过一番研究,我发现如果我使用以下属性

[ProducesResponseType(typeof(Models.Customer), StatusCodes.Status200OK)]
[HttpGet("{Id}/customer")]
public async Task<IActionResult> GetCustomer(int Id)

Displays Schemas block in the swagger UI which is exactly I want.在 swagger UI 中显示Schemas块,这正是我想要的。 However I don't want to go through each of my Controller Get / Post methods and add this attribute.但是,我不想遍历我的每个 Controller Get / Post 方法并添加此属性。 It always worked without this but what could be preventing this from working out of the box?它总是在没有这个的情况下工作,但是什么可以阻止它开箱即用?

Swashbuckle creates the model based on the action's return type. Swashbuckle 根据操作的返回类型创建模型。 You have several options:您有多种选择:

  • You can return the actual type (eg public async Task<Models.Customer> GetCustomer(int Id)您可以返回实际类型(例如public async Task<Models.Customer> GetCustomer(int Id)

  • If you return an IActionResult , you can use the ProducesResponseType attribute如果返回IActionResult ,则可以使用ProducesResponseType属性

  • You can return an ActionResult<T> which works just like the IActionResult but with the actual type您可以返回一个ActionResult<T> ,它的工作方式与IActionResult类似,但具有实际类型

You can check the documentation for more information: https://docs.microsoft.com/en-us/aspnet/core/web-api/action-return-types?view=aspnetcore-3.1您可以查看文档以获取更多信息: https : //docs.microsoft.com/en-us/aspnet/core/web-api/action-return-types?view=aspnetcore-3.1

So, your method returns IActionResult , it is a common thing and the compiler cannot see what real result will be returned.因此,您的方法返回IActionResult ,这是很常见的事情,编译器看不到将返回什么实际结果。 That's why you should use ProducesResponseType .这就是您应该使用ProducesResponseType的原因。

If you want to use IActionResult and Asp.Net MVC controllers it is only this way to use.如果您想使用IActionResult和 Asp.Net MVC 控制器,则只能以这种方式使用。

But another point is: do you really need Asp.Net MVC controllers?但另一点是:你真的需要 Asp.Net MVC 控制器吗? And if you need Asp.Net MVC controllers why do you want to create swagger documentation?如果您需要 Asp.Net MVC 控制器,为什么要创建 swagger 文档? These things are contradictionary.这些事情是矛盾的。

Swagger needs to create Public Api Documentation. Swagger 需要创建公共 Api 文档。 To add possibility for external programmers to use your API.添加外部程序员使用您的 API 的可能性。 And in this case it is better to use Api controllers.在这种情况下,最好使用 Api 控制器。

If you need to use Asp.Net MVC controllers with that features like Autorization, then you don't need to create Swagger, because it should be used in your own project.如果您需要使用具有 Autorization 等功能的 Asp.Net MVC 控制器,那么您不需要创建 Swagger,因为它应该在您自己的项目中使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM