Swagger 2.0-动作错误的HTTP方法不明确

时间:2019-07-20 08:26:55

标签: c# asp.net-core swagger

我正在将swagger 2.0用于我的api测试和文档。我的api控制器正在使用Dispose()方法来释放非托管资源。但是招摇不适用于Dispose方法,并且给NotSupportedException: Ambiguous HTTP method for action - ProfileCore.Controllers.UserTypeController.Dispose1 (ProfileCore). Actions require an explicit HttpMethod binding for Swagger 2.0

摇动代码:

c.SwaggerDoc("v1", new Info
            {
                Version = "v1",
                Title = "Profile Core API",
                Description = "A simple example ASP.NET Core Web API",
                TermsOfService = "None",
                Contact = new Contact
                {
                    Name = "Profile Core",
                    Email = string.Empty,
                    Url = "https://example.com"
                },
                License = new License
                {
                    Name = "Use under LICX",
                    Url = "https://example.com/license"
                }
            });

            c.AddSecurityDefinition("Bearer", new ApiKeyScheme { In = "header", Description = "Please enter JWT with Bearer into field", Name = "Authorization", Type = "apiKey" });
            c.AddSecurityRequirement(new Dictionary<string, IEnumerable<string>> {
            { "Bearer", Enumerable.Empty<string>() },
            });

            // Set the comments path for the Swagger JSON and UI.
            var xmlFile = $"{Assembly.GetEntryAssembly().GetName().Name}.xml";
            var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
            c.IncludeXmlComments(xmlPath); 
        });

API控制器:

[ApiController]
public class UserTypeController : ControllerBase
{
    #region Fields
    private readonly IUserTypeBiz _userTypeBiz;
    #endregion

    #region Ctor
    public UserTypeController(IUserTypeBiz userTypeBiz)
    {
        _userTypeBiz = userTypeBiz;
    }
    #endregion

    #region Actions
    /// <summary>
    /// Get All User Type List
    /// </summary>
    /// <returns></returns>
    [HttpGet("getList")]
    public IActionResult List()
    {
        return Ok(_userTypeBiz.List(null).Value);
    }
    #endregion

    #region Dispose Methods
    private bool _disposed = false;
    /// <summary>
    /// Dispose the UserTypeBiz after finish task
    /// </summary>
    /// <param name="disposing">Flag for indicating desposing or not</param>
    public virtual void Dispose(bool disposing)
    {
        if (!_disposed)
        {
            if (disposing)
            {
                _userTypeBiz.Dispose();
            }
        }
        _disposed = true;
    }

    /// <summary>
    /// Dispose the UserTypeBiz after finish task
    /// </summary>
    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }
    #endregion
}

1 个答案:

答案 0 :(得分:1)

您需要告诉Swagger Dispose不是动作。一种简单的方法是使用[NonAction]属性:

[NonAction]
public virtual void Dispose(bool disposing)
// ...

[NonAction]
public void Dispose()
// ...