我在示例 C# ASP.NET Core 2.0 Api 中有一个 Authors 控制器,并且我正在使用 Swashbuckle 生成 Swagger .json。
当我在 AuthorsController 中包含以下两个方法时,不会生成 .json
[HttpPost(Name = "CreateAuthor")]
public IActionResult CreateAuthor([FromBody] AuthorForCreationDto author)
{
return null //for simplicity repeating the problem
}
和
[HttpPost(Name = "CreateAuthorWithDateOfDeath")]
public IActionResult CreateAuthorWithDateOfDeath(
[FromBody] AuthorForCreationWithDateOfDeathDto author)
{
return null
}
然后当我尝试访问 Swagger UI 时,我得到了
加载API定义失败。 未定义./v1/swagger.json
但是,如果我注释掉任一方法,.json 将生成。
在启动配置服务中我有
services.AddSwaggerGen(c => {
c.OperationFilter<AuthorizationHeaderParameterOperationFilter>();
c.SwaggerDoc("v1", new Info
{
Version = "v1",
Title = "track3 API",
Description = "ASP.NET Core Web API",
TermsOfService = "None",
Contact = new Contact
{
Name = "my name",
Email = "[email protected]"
}
});
});
哪里
public class AuthorizationHeaderParameterOperationFilter : IOperationFilter
{
public void Apply(Operation operation, OperationFilterContext context)
{
var filterPipeline = context.ApiDescription.ActionDescriptor.FilterDescriptors;
var isAuthorized = filterPipeline.Select(filterInfo => filterInfo.Filter).Any(filter => filter is AuthorizeFilter);
var allowAnonymous = filterPipeline.Select(filterInfo => filterInfo.Filter).Any(filter => filter is IAllowAnonymousFilter);
if (isAuthorized && !allowAnonymous)
{
if (operation.Parameters == null)
operation.Parameters = new List<IParameter>();
operation.Parameters.Add(new NonBodyParameter
{
Name = "Authorization",
In = "header",
Description = "access token",
Required = true,
Type = "string"
});
}
}
}
在配置中我有
app.UseSwaggerUI(c =>
{
c.RoutePrefix = "api-docs";
c.SwaggerEndpoint("./v1/swagger.json", "Api v1");
});
为什么会这样?
[更新]
控制器中还有第二个类似的方法。 如果我注释掉第二个方法并取消注释第一个方法,则会生成 .json。 这两种方法都不会出现在 Swagger 中
这是 Dto 的代码
public class AuthorForCreationDto
{
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTimeOffset DateOfBirth { get; set; }
public string Genre { get; set; }
public ICollection<BookForCreationDto> Books { get; set; }
= new List<BookForCreationDto>();
}
public class AuthorForCreationWithDateOfDeathDto
{
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTimeOffset DateOfBirth { get; set; }
public DateTimeOffset? DateOfDeath { get; set; }
public string Genre { get; set; }
}
public class BookForCreationDto : BookForManipulationDto
{
}
public abstract class BookForManipulationDto
{
[Required(ErrorMessage = "You should fill out a title.")]
[MaxLength(100, ErrorMessage = "The title shouldn't have more than 100 characters.")]
public string Title { get; set; }
[MaxLength(500, ErrorMessage = "The description shouldn't have more than 500 characters.")]
public virtual string Description { get; set; }
}
参见:Swashbuckle 在 asp.net core 中失败并出现 NotSupportedException 异常
将您的帖子方法命名为:
[HttpPost("DistinctName")]
public IActionResult DistinctName()
{
}
[HttpPost("DistinctName2")]
public IActionResult DistinctName2()
{
}
使用梅尔的回答很有帮助,但并没有为我解决所有问题。我有一个 .NetCore 项目。
我使用了以下代码,Swagger 工作得很好。
using Microsoft.AspNetCore.Mvc;
[HttpPost]
[Route("MethodTheFirst")]
public IActionResult MethodTheFirst(string name, int id)
{
}
[HttpPost]
[Route("MethodTheSecond")]
public IActionResult MethodTheSecond(string name)
{
}