首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Swashbuckle.AspNetCore <example>不是为我工作的

Swashbuckle.AspNetCore <example>不是为我工作的
EN

Stack Overflow用户
提问于 2022-11-11 20:31:09
回答 1查看 119关注 0票数 0

我正在尝试使用标记来建立示例,但是,它并不适合我。我正在使用Swashbuckle.AspNetCore库。

下面是一些代码示例,

  1. 在我的csproj中添加了以下内容。文件

代码语言:javascript
复制
<GenerateDocumentationFile>true</GenerateDocumentationFile>

  1. Program.cs

代码语言:javascript
复制
            builder.Services.AddSwaggerGen(options =>
            {
                options.OperationFilter<SwaggerDefaultValues>();
                var filePath = Path.Combine(System.AppContext.BaseDirectory, "My.xml");
                options.IncludeXmlComments(filePath);

            });

  1. Controller看起来像,

代码语言:javascript
复制
[Authorize]
[ApiController]
[ApiVersion("1.0")]
[Route("[controller]")]
[Route("v{version:apiVersion}/[controller]")]
[Produces("application/json")]
public class MyController : ControllerBase
{
}

  1. 我的方法看起来像,

代码语言:javascript
复制
/// <summary>
    /// Method to Post an Incident
    /// </summary>
    /// <param name="initiateRequest" example="FORM_CODE"></param>
    [ProducesResponseType(typeof(InitiateResponseDTO), StatusCodes.Status201Created)]
    [ProducesResponseType(typeof(ExceptionDetails), StatusCodes.Status400BadRequest)]
    [ProducesResponseType(StatusCodes.Status401Unauthorized)]
    [ProducesResponseType(StatusCodes.Status500InternalServerError)]
    [HttpPost]
public async Task<ActionResult<InitiateResponseDTO>> PostAsync(MyRequest myRequest)
    {
//Logic

        return StatusCode(StatusCodes.Status201Created, InitiateResponseDTO);
    }

  1. MyDTO看起来像,

代码语言:javascript
复制
/// <summary>
/// Initiate Response DTO
/// </summary>
public class InitiateResponseDTO
{
    /// <summary>
    /// IncidentId 
    /// </summary>
    /// <example>985769890</example>
    public int IncidentId { get; set; }
}

  1. 异常详细信息类

代码语言:javascript
复制
/// <summary>
    /// Exception Details class
    /// </summary>
    public class ExceptionDetails
    {
        /// <summary>
        /// HTTP status code for the exception.
        /// </summary>
        /// <example>400</example>
        public int StatusCode { get; set; } = (int)HttpStatusCode.InternalServerError;

        /// <summary>
        /// (Friendly) message about the exception.
        /// </summary>
        /// <example>Invalid form code supplied: FORM_CODE</example>
        public string Message { get; set; } = "An error occured";

        /// <summary>
        /// Error code for the returned error.
        /// </summary>
        /// <example>UNKNOWN_FORM</example>
        public string ErrorCode { get; set; } = ErrorCodes.Generic;

        /// <summary>
        /// Exception Target denoting the method in which the error occurred.
        /// </summary>
        /// <example>MoveNext <- Method name from which the error occurred</example>
        public string? Target { get; set; }

        /// <summary>
        /// InnerError message denoting the StackTrace.
        /// </summary>
        /// <example> Some Sample</example>
public string? InnerError { get; set; }
}

在Swagger中,我没有看到示例值,

配置有什么不正确吗?我使用的是Swashbuckle.AspNetCore 6.4.0

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-11-14 06:04:41

一个简单的选择是用DefaultValue来装饰你的房产。

代码语言:javascript
复制
/// <summary>
/// Initiate Response DTO
/// </summary>
public class InitiateResponseDTO
{
    /// <summary>
    /// IncidentId 
    /// </summary>
    /// <example>985769890</example>
    [DefaultValue(985769890)]
    public int IncidentId { get; set; }
}

根据序列化是如何在您的项目中完成,它应该工作。

也有可能查看Swashbuckle.AspNetCore.Filters Nuget库来设置您喜欢的示例,但是它需要更多的工作:https://github.com/mattfrear/Swashbuckle.AspNetCore.Filters#automatic-annotation

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74407822

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档