首页 > Windows开发 > 详细

NSwag给api加上说明

时间:2019-02-28 13:28:24      阅读:994      评论:0      收藏:0      [点我收藏+]

参考http://petstore.swagger.io

 

给controller加上description

https://github.com/RSuter/NSwag/issues/1803

 

xml summary

https://github.com/RSuter/NJsonSchema/wiki/XML-Documentation

 

WebApiToSwaggerGenerator

The WebApiToSwaggerGenerator class is used to generate a Swagger specification from ASP.NET Web API controllers:

var settings = new WebApiToSwaggerGeneratorSettings 
{ 
    DefaultUrlTemplate = "api/{controller}/{action}/{id}" 
};
var generator = new WebApiToSwaggerGenerator(settings);
var document = await generator.GenerateForControllerAsync<PersonsController>();
var swaggerSpecification = document.ToJson();

The generator internally uses the JsonSchemaGenerator class (NJsonSchema project) to generate the JSON Schemas of the request and response DTO types.

To generate the Swagger specification for Web API controllers in an external .NET assembly, use the WebApiAssemblyToSwaggerGenerator class.

ASP.NET Core

Important: This reflection based generator will eventually be deprecated by the new APi Explorer based generator AspNetCoreToSwaggerGenerator!

When generating a Swagger specification for an ASP.NET Core application, set the IsAspNetCore setting to true.

Supported ASP.NET Web API attributes

  • Controller classes:
    • RoutePrefixAttribute on the controller class
    • DescriptionAttribute on the controller class (used to fill the Swagger ‘Summary‘)
  • Action methods:
    • RouteAttribute and ActionNameAttribute on the action method, with support for Route constraints (e.g. [Route("users/{id:int}"])
    • DescriptionAttribute on the action method (used as Swagger operation ‘Description‘)
    • One or more ProducesResponseTypeAttribute on the action method
    • HTTP verb attributes:
      • AcceptVerbsAttribute
      • HttpGetAttribute
      • HttpPostAttribute
      • HttpPutAttribute
      • HttpDeleteAttribute
      • HttpOptionsAttribute
  • Action method parameters:
    • FromBodyAttribute on the action method parameter
    • ModelBinderAttribute

The generator also supports data annotations, check out this page for more information.

 

Additionally supported attributes

  • Package: NSwag.Annotations

SwaggerResponseAttribute(httpAction, type)

Defines the response type of a Web API action method and HTTP action. See Specify the response type of an action.

SwaggerDefaultResponseAttribute()

Adds the default response (HTTP 200/204) based on the return type of the operation method. This can be used in conjunction with the SwaggerResponseAttribute or another response defining attribute (ProducesResponseTypeAttribute, etc.). This is needed because if one of these attributes is available, you have to define all responses and the default response is not automatically added. If an HTTP 200/204 response is already defined then the attribute is ignored (useful if the attribute is defined on the controller or the base class).

SwaggerIgnoreAttribute()

Excludes a Web API method from the Swagger specification.

SwaggerOperationAttribute(operationId)

Defines a custom operation ID for a Web API action method.

SwaggerTagsAttribute(tags)

Defines the operation tags. See Specify the operation tags.

SwaggerExtensionDataAttribute()

Adds extension data to the document (when applied to a controller class), an operation or parameter.

  • Package: NJsonSchema

NotNullAttribute and CanBeNullAttribute

Can be defined on DTO properties (handled by NJsonSchema), operation parameters and the return type with:

[return: NotNull]
public string GetName()

The default behavior can be changed with the WebApiToSwaggerGeneratorSettings.DefaultReferenceTypeNullHandling setting (default: Null).

 

NSwag给api加上说明

原文:https://www.cnblogs.com/chucklu/p/10449395.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!