Swagger“规范中没有定义任何操作!”通用基础控制器问题

问题描述 投票:0回答:1

我有一个接口,

IItemController<T>
,应用于基础
ItemController<T>
,它也继承了
ControllerBase
.

public class ItemController<T> : ControllerBase, IItemController<T> where T : class {
    private readonly IManager<T> _manager;
    private readonly ILogger<ItemController<T>> _logger;

    public ItemController(ILogger<ItemController<T>> logger, IManager<T> manager)
    {
        _logger = logger;
        _manager = manager;
    }

    [HttpGet($@"test")]
    public IActionResult Test(){
        return NoContent();
    }

    /* Other Methods */
}

然后我有一个

ProductController<T>
继承
ItemController<T>
指定
Product
类型:

[ApiController]
[Route("[controller]")]
public class ProductController<T> : ItemController<T> where T : Product
{
    private readonly IManager<T> _manager;
    private readonly ILogger<ProductController<T>> _logger;

    public ProductController(ILogger<ProductController<T>> logger, IManager<T> manager) : base(logger, manager)
    {
        _logger = logger;
        _manager = manager;
    }
}

我假设这应该为我继承

ItemController
的每个控制器生成招摇,但我得到:

“规范中没有定义任何操作!”

我尝试直接导航到 Urls,但它们似乎也不起作用,所以我可能遇到了一个不自大的问题。

这是我的 Program.cs

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddControllers();

// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddEntityFrameworkSqlite().AddDbContext<DataContext>(opt =>
    opt.UseSqlite(builder.Configuration.GetConnectionString("SqliteConnection")));

builder.Services.AddTransient(typeof(IManager<>), typeof(Manager<>));
builder.Services.AddTransient(typeof(IPager<>), typeof(Pager<>));
builder.Services.AddTransient(typeof(IRepository<>), typeof(Repository<>));
builder.Services.AddTransient(typeof(IItemController<>), typeof(ItemController<>));

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseHttpsRedirection();
app.MapControllers();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints => endpoints.MapControllers());

app.Run();
c# asp.net-core generics inheritance swagger-ui
1个回答
1
投票

我实际上简化了一点。我保持

ItemController
Program.cs
不变,并像这样更改了
ProductController
类:

[ApiController]
[Route("[controller]")]
public class ProductController : ItemController<Product>
{
    public ProductController(ILogger<ProductController> logger, IManager<Product> manager) : base(logger, manager)
    {
    }
}

我不确定为什么另一种方法不起作用,但无论如何这更简洁。

© www.soinside.com 2019 - 2024. All rights reserved.