seb*_*ebu 2 c# swagger asp.net-core
我正在使用 swagger 2.0 进行 api 测试和文档。我的 api 控制器正在使用 Dispose() 方法来释放非托管资源。但 swagger 不适用于 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);
});
Run Code Online (Sandbox Code Playgroud)
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
}
Run Code Online (Sandbox Code Playgroud)
您需要告诉 Swagger 这Dispose不是 Action。一个简单的方法是使用[NonAction]属性:
[NonAction]
public virtual void Dispose(bool disposing)
// ...
[NonAction]
public void Dispose()
// ...
Run Code Online (Sandbox Code Playgroud)