C# 控制器中的多个HttpPost方法防止生成swagger.json
我在一个示例C#ASP.NETCore2.0API中有一个Authors控制器,我正在使用Swashback生成Swagger.json 当我在AuthorsController中包含以下两个方法时,.json不会生成C# 控制器中的多个HttpPost方法防止生成swagger.json,c#,asp.net-core,swagger,swashbuckle,C#,Asp.net Core,Swagger,Swashbuckle,我在一个示例C#ASP.NETCore2.0API中有一个Authors控制器,我正在使用Swashback生成Swagger.json 当我在AuthorsController中包含以下两个方法时,.json不会生成 [HttpPost(Name = "CreateAuthor")] public IActionResult CreateAuthor([FromBody] AuthorForCreationDto author) { return null
[HttpPost(Name = "CreateAuthor")]
public IActionResult CreateAuthor([FromBody] AuthorForCreationDto author)
{
return null //for simplicity repeating the problem
}
及
然后,当我尝试访问招摇过市的用户界面时,我得到
未能加载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"
}
});
});
为什么会这样
[更新]
控制器中还有第二种类似的方法。
如果我注释掉第二个方法并取消注释第一个方法,那么将生成.json。
这两种方法都不会大摇大摆地出现
这是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; }
}
公共类authorForCreationTo
{
公共字符串名{get;set;}
公共字符串LastName{get;set;}
public DateTimeOffset出生日期{get;set;}
公共字符串类型{get;set;}
公共ICollection图书{get;set;}
=新列表();
}
创建日期为DateOfDeathTo的公共类AuthorForCreationWithDateOfDeathTo
{
公共字符串名{get;set;}
公共字符串LastName{get;set;}
public DateTimeOffset出生日期{get;set;}
公共DateTimeOffset?死亡日期{get;set;}
公共字符串类型{get;set;}
}
用于创建的公共类Book To:BookForman操作To
{
}
公共抽象类BookForManipulationDto
{
[必需(ErrorMessage=“您应该填写标题。”)]
[MaxLength(100,ErrorMessage=“标题不应超过100个字符。”)]
公共字符串标题{get;set;}
[MaxLength(500,ErrorMessage=“描述不应超过500个字符。”)]
公共虚拟字符串描述{get;set;}
}
您收到的错误消息是什么?如果您直接请求swagger.json,您将看到一条错误消息。您是如何在启动时配置Swashbuckle的?我假设如果您删除该控制器,一切正常,对吗。。。你能分享你的作者的代码吗?如果我是你的话,我会把post操作转移到自己的controllers上,这是一个很好的解决办法,但是仍然有一些事情在进行
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");
});
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; }
}