Kir*_*eed 5 c# swagger swashbuckle asp.net-core
我在示例 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
}
Run Code Online (Sandbox Code Playgroud)
和
[HttpPost(Name = "CreateAuthorWithDateOfDeath")]
public IActionResult CreateAuthorWithDateOfDeath(
[FromBody] AuthorForCreationWithDateOfDeathDto author)
{
return null
}
Run Code Online (Sandbox Code Playgroud)
然后当我尝试访问 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 = "myemail@mydomain.com"
}
});
});
Run Code Online (Sandbox Code Playgroud)
在哪里
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"
});
}
}
}
Run Code Online (Sandbox Code Playgroud)
在配置中我有
app.UseSwaggerUI(c =>
{
c.RoutePrefix = "api-docs";
c.SwaggerEndpoint("./v1/swagger.json", "Api v1");
});
Run Code Online (Sandbox Code Playgroud)
为什么会这样呢?
[更新]
控制器中还有第二个类似的方法。如果我注释掉第二个方法并取消注释第一个方法,则会生成 .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; }
}
Run Code Online (Sandbox Code Playgroud)
小智 2
请参阅:Swashbuckle 在 asp.net core 中失败并出现 NotSupportedException 异常
将您的帖子方法命名为:
[HttpPost("DistinctName")]
public IActionResult DistinctName()
{
}
[HttpPost("DistinctName2")]
public IActionResult DistinctName2()
{
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3113 次 |
| 最近记录: |