مستند سازی ASP.NET Core 2x API توسط OpenAPI Swagger - قسمت سوم - تکمیل مستندات یک API با کامنتها
نویسنده: وحید نصیری
تاریخ: ۱۳۹۸/۰۱/۲۹ ۱۳:۲۵
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
/// <summary>
/// Get an author by his/her id
/// </summary>
/// <param name="authorId">The id of the author you want to get</param>
/// <returns>An ActionResult of type Author</returns>
[HttpGet("{authorId}")]
public async Task<ActionResult<Author>> GetAuthor(Guid authorId) <Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
</PropertyGroup> namespace OpenAPISwaggerDoc.Web
{
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddSwaggerGen(setupAction =>
{
setupAction.SwaggerDoc(
// ...
);
var xmlCommentsFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
var xmlCommentsFullPath = Path.Combine(AppContext.BaseDirectory, xmlCommentsFile);
setupAction.IncludeXmlComments(xmlCommentsFullPath);
});
}
using System;
namespace OpenAPISwaggerDoc.Models
{
/// <summary>
/// An author with Id, FirstName and LastName fields
/// </summary>
public class Author
{
/// <summary>
/// The id of the author
/// </summary>
public Guid Id { get; set; }
/// <summary>
/// The first name of the author
/// </summary>
public string FirstName { get; set; }
/// <summary>
/// The last name of the author
/// </summary>
public string LastName { get; set; }
}
} <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
</PropertyGroup>
</Project> namespace OpenAPISwaggerDoc.Web
{
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddSwaggerGen(setupAction =>
{
setupAction.SwaggerDoc(
// ...
);
var xmlFiles = Directory.GetFiles(AppContext.BaseDirectory, "*.xml", SearchOption.TopDirectoryOnly).ToList();
xmlFiles.ForEach(xmlFile => setupAction.IncludeXmlComments(xmlFile));
});
}
[HttpPut("{authorId}")]
public async Task<ActionResult<Author>> UpdateAuthor(Guid authorId, AuthorForUpdate authorForUpdate) using System.ComponentModel.DataAnnotations;
namespace OpenAPISwaggerDoc.Models
{
/// <summary>
/// An author for update with FirstName and LastName fields
/// </summary>
public class AuthorForUpdate
{
/// <summary>
/// The first name of the author
/// </summary>
[Required]
[MaxLength(150)]
public string FirstName { get; set; }
/// <summary>
/// The last name of the author
/// </summary>
[Required]
[MaxLength(150)]
public string LastName { get; set; }
}
}
[HttpPut("{authorId}")]
public async Task<ActionResult<Author>> UpdateAuthor(Guid authorId, AuthorForUpdate authorForUpdate) [HttpPatch("{authorId}")]
public async Task<ActionResult<Author>> UpdateAuthor(
Guid authorId,
JsonPatchDocument<AuthorForUpdate> patchDocument)
/// <summary>
/// Partially update an author
/// </summary>
/// <param name="authorId">The id of the author you want to get</param>
/// <param name="patchDocument">The set of operations to apply to the author</param>
/// <returns>An ActionResult of type Author</returns>
/// <remarks>
/// Sample request (this request updates the author's first name) \
/// PATCH /authors/id \
/// [ \
/// { \
/// "op": "replace", \
/// "path": "/firstname", \
/// "value": "new first name" \
/// } \
/// ] \
/// </remarks>
[HttpPatch("{authorId}")]
public async Task<ActionResult<Author>> UpdateAuthor(
Guid authorId,
JsonPatchDocument<AuthorForUpdate> patchDocument)
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<TreatWarningsAsErrors>false</TreatWarningsAsErrors>
<WarningsAsErrors>NU1605;</WarningsAsErrors>
<NoWarn>1701;1702;1591</NoWarn>
</PropertyGroup> [HttpPost("two-files")]
public async Task Upload(IFormFile file1, IFormFile file2)
{
// validate the files, scan virus, save them to a file storage
}
و یا حالت پیچیدهتر آن
/// <summary>
/// Submit a form which contains a key-value pair and a file.
/// </summary>
/// <param name="id">Student ID</param>
/// <param name="form">A form which contains the FormId and a file</param>
/// <returns></returns>
[HttpPost("{id:int}/forms")]
[ProducesResponseType(typeof(FormSubmissionResult), StatusCodes.Status201Created)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public async Task<ActionResult<FormSubmissionResult>> SubmitForm(int id, [FromForm] StudentForm form)
{
// ...
}
public class StudentForm
{
[Required] public int FormId { get; set; }
[Required] public IFormFile StudentFile { get; set; }
}