我有两个继承其他控制器的控制器 - 对于其中一个,生成了swagger doc,而另一个则不生成。 我的控制器是:
[Route("Actor")]
public class ActorController : BaseDefControllerLog<Actor>
{
public ActorController(BaseDefController<Actor> baseController, ILogger<ActorController> logger, IAppConfig appSettings) : base(baseController, logger, appSettings)
{ }
}
[Route("ActorCatalog")]
public class ActorCatalogController: LoggerController, ICatalogController<Actor>
{
private readonly BaseDefControllerLog<Actor> _baseController;
public ActorCatalogController(BaseDefControllerLog<Actor> baseController, ILogger<ActorCatalogController> logger) : base(logger)
{
_baseController = baseController;
}
[Route("GetAll")]
[HttpGet]
public ICollection<Actor> GetAll(int? pageNumber = null, int? pageSize = null)
{
//GetAll logic
}
}
下面是BaseDefController,BaseDefControllerLog和LoggerController:
public class BaseDefController<T> : Controller, ICatalogController<T> where T : class, IConfigDef
{
private readonly IReadManager<T> _manager;
public BaseDefController(IReadManager<T> manager)
{
_manager = manager;
}
[HttpGet]
public virtual ICollection<T> GetAll(int? pageNumber = null, int? pageSize = null)
{
return _manager.GetAll(pageNumber, pageSize);
}
}
public class BaseDefControllerLog<T>: LoggerController where T : class, IConfigDef
{
private readonly BaseDefController<T> _baseController;
private readonly ILogger<BaseDefControllerLog<T>> _logger;
public BaseDefControllerLog(BaseDefController<T> baseController, ILogger<BaseDefControllerLog<T>> logger, IAppConfig appSettings) : base(logger)
{
_baseController = baseController;
_logger = logger;
}
[HttpGet]
public virtual ICollection<T> GetAll(string remoteIpAddress = null, int? pageNumber = null, int? pageSize = null)
{
//GetAll logic
}
}
public class LoggerController : Controller
{
protected ILogger Logger;
public LoggerController(ILogger logger)
{
Logger = logger;
}
public override void OnActionExecuting(ActionExecutingContext context)
{ //... }
public override void OnActionExecuted(ActionExecutedContext context)
{ //... }
}
&#34;演员&#34; model是一个简单的类:
public class Actor : IConfigDef
{
public int Id { get; set; }
public Guid Tag { get; set; }
public string Name { get; set; }
public DateTime LastUpdated { get; set; }
public bool Deleted { get; set; }
}
Swashbuckle.AspNetCore 1.0.0
我使用Route属性修饰了ActorController和ActorCatalogController。此时,swagger在生成json时抛出异常。如果我在ActorController上删除路由,因此只为ActorCatalogController生成文档,一切正常。如果我装饰继承的控制器&#39;使用路由属性的动作,我也在json上得到500响应。我也尝试删除虚拟关键字,没有运气。
两个控制器的文档(ActorCatalogController和ActorController)。
500:http://localhost:4100/swagger/v1/swagger.json 无法生成JSON。我抓住了这个例外:&#34;抛出异常:&#39; System.NotSupportedException&#39;在Swashbuckle.AspNetCore.SwaggerGen.dll&#34;。
我的项目的目标框架是.NET framework 4.6.2。我使用Microsoft.AspNetCore 1.1.2和Microsoft.AspNetCore.Hosting.WindowsServices 1.1.2。 我在Startup.cs中的招摇配置如下:
public void ConfigureServices(IServiceCollection services)
{
services
.AddMvc(config =>
{
config.RespectBrowserAcceptHeader = true;
})
.AddJsonOptions(options =>
{
options.SerializerSettings.TypeNameHandling = TypeNameHandling.Auto;
})
.AddXmlSerializerFormatters();
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new Info { Title = "InternalServer", Version = "v1" });
});
InitializeContainer(services);
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
app.UseMvc();
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("v1/swagger.json", "SwaggerDemo V1");
});
LogRegistration(loggerFactory);
}
答案 0 :(得分:0)
我在这里解决了问题,有关变通方法的更多详细信息:https://github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/511