【发布时间】:2025-11-30 02:15:01
【问题描述】:
我有一个带有 CRUD 操作的通用 WebApi 控制器,如下所示:
public abstract class BaseController<TEntity> : ApiController where TEntity : class
{
protected abstract DbSet<TEntity> DatabaseSet { get; }
// GET: api/{entity}
[Route("")]
public IEnumerable<TEntity> GetAll()
{
return DatabaseSet;
}
// GET: api/{entity}/5
[Route("{id:int}")]
//[ResponseType(TEntity)] ToDo: is this possible? <---
public IHttpActionResult Get(int id)
{
TEntity entity = DatabaseSet.Find(id);
if (entity == null)
{
return NotFound();
}
return Ok(entity);
}
// ...
// and the rest
// ...
}
我的问题是关于被注释掉的 [ResponseType(TEntity)]。这条线不起作用。也不使用 typeof(TEntity)。错误是'属性参数不能使用类型参数'有没有办法让泛型类型知道 ResponseType?
谢谢! 碧玉
【问题讨论】:
标签: generics asp.net-web-api attributes