C# 使用自定义WebApi控制器时出现问题
我已经为我的项目创建了一个自定义基C# 使用自定义WebApi控制器时出现问题,c#,asp.net,asp.net-mvc-4,asp.net-web-api,C#,Asp.net,Asp.net Mvc 4,Asp.net Web Api,我已经为我的项目创建了一个自定义基ApiController: public abstract class MyApiControllerBase : ApiController { private IContextService<DomainUnitOfWork> ContextService; private UnitOfWorkScopeBase<DomainUnitOfWork> _unitOfWorkScope; private bo
ApiController
:
public abstract class MyApiControllerBase : ApiController
{
private IContextService<DomainUnitOfWork> ContextService;
private UnitOfWorkScopeBase<DomainUnitOfWork> _unitOfWorkScope;
private bool _autoDispose;
public bool DisposeUnitOfWorkOnResultExecuted
{
get { return this._autoDispose; }
set { this._autoDispose = value; }
}
protected DomainUnitOfWork UnitOfWork
{
get { return UnitOfWorkScope.Current; }
}
private UnitOfWorkScopeBase<DomainUnitOfWork> UnitOfWorkScope
{
get
{
if (this._unitOfWorkScope == null)
{
this._unitOfWorkScope = new PerRequestUnitOfWorkScope<DomainUnitOfWork>(this.LightSpeedContext);
}
return this._unitOfWorkScope;
}
}
private void DisposeUnitOfWorkScope()
{
if (this._autoDispose && this._unitOfWorkScope != null && this._unitOfWorkScope.HasCurrent)
{
DomainUnitOfWork current = this._unitOfWorkScope.Current;
current.Dispose();
}
}
public override Task<HttpResponseMessage> ExecuteAsync(HttpControllerContext controllerContext, CancellationToken cancellationToken)
{
return base.ExecuteAsync(controllerContext, cancellationToken)
.ContinueWith<HttpResponseMessage>(ant =>
{
DisposeUnitOfWorkScope();
return ant.Result;
});
}
public MyApiControllerBase(IContextService<DomainUnitOfWork> contextService)
{
ContextService = contextService;
this._autoDispose = true;
}
~MyApiControllerBase()
{
Dispose(false);
}
public new void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
DisposeUnitOfWorkScope();
ContextService = null;
}
base.Dispose(disposing);
}
protected LightSpeedContext<DomainUnitOfWork> LightSpeedContext
{
get { return ContextService.Context; }
}
}
公共抽象类MyApiControllerBase:ApiController
{
私有IContextService上下文服务;
专用unitOfWorkScope库_unitOfWorkScope;
私人楼宇自动处置;
公共布尔处理工作结果已执行
{
获取{返回此。\u autoDispose;}
设置{this.\u autoDispose=value;}
}
受保护域UnitOfWork UnitOfWork
{
获取{return UnitOfWorkScope.Current;}
}
专用UnitOfWorkScope基本UnitOfWorkScope
{
得到
{
if(this.\u unitOfWorkScope==null)
{
this.\u unitOfWorkScope=new PerRequestUnitOfWorkScope(this.LightSpeedContext);
}
返回此。\u unitOfWorkScope;
}
}
私有void DisposeUnitOfWorkScope()
{
if(this.\u autoDispose&&this.\u unitOfWorkScope!=null&&this.\u unitOfWorkScope.HasCurrent)
{
DomainUnitOfWork current=此。\u unitOfWorkScope.current;
current.Dispose();
}
}
公共覆盖任务ExecuteAsync(HttpControllerContext controllerContext,CancellationToken CancellationToken)
{
return base.ExecuteAsync(controllerContext,cancellationToken)
.ContinueWith(ant=>
{
DisposeUnitOfWorkScope();
返回蚂蚁结果;
});
}
公共MyApiControllerBase(IContextService contextService)
{
ContextService=ContextService;
这是。_autoDispose=true;
}
~MyApiControllerBase()
{
处置(虚假);
}
公开新的无效处置()
{
处置(真实);
总干事(本);
}
受保护的覆盖无效处置(布尔处置)
{
如果(处置)
{
DisposeUnitOfWorkScope();
ContextService=null;
}
基地。处置(处置);
}
受保护的LightSpeedContext LightSpeedContext
{
获取{return ContextService.Context;}
}
}
接下来,我创建了一个从MyApiControllerBase派生的控制器:
public class MyApiController : MyApiControllerBase
{
(...)
[ApiExplorerSettings(IgnoreApi = false)]
public IEnumerable<MyDto> Get()
{
(...)
}
[ApiExplorerSettings(IgnoreApi = false)]
public IEnumerable<MyDto> Get(int pageSize)
{
(...)
}
[ApiExplorerSettings(IgnoreApi = false)]
public IEnumerable<MyDto> Get(int pageIdx, int pageSize)
{
(...)
}
[ApiExplorerSettings(IgnoreApi = false)]
public IEnumerable<MyDto> Get(string id)
{
(...)
}
[ApiExplorerSettings(IgnoreApi = false)]
public IEnumerable<MyDto> Get(string id, int pageSize)
{
(...)
}
[ApiExplorerSettings(IgnoreApi = false)]
public IEnumerable<MyDto> Get(string id, int pageIdx, int pageSize)
{
(...)
}
[ApiExplorerSettings(IgnoreApi = false)]
public HttpResponseMessage Post(MyDto dto)
{
(...)
}
public MyApiController(
IContextService<DomainUnitOfWork> contextService)
: base(contextService)
{
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
(...)
}
base.Dispose(disposing);
}
}
公共类MyApiController:MyApiControllerBase
{
(...)
[ApiExplorerSettings(IgnoreApi=false)]
公共IEnumerable Get()
{
(...)
}
[ApiExplorerSettings(IgnoreApi=false)]
公共IEnumerable Get(int pageSize)
{
(...)
}
[ApiExplorerSettings(IgnoreApi=false)]
公共IEnumerable Get(int pageIdx,int pageSize)
{
(...)
}
[ApiExplorerSettings(IgnoreApi=false)]
公共IEnumerable Get(字符串id)
{
(...)
}
[ApiExplorerSettings(IgnoreApi=false)]
公共IEnumerable Get(字符串id,int pageSize)
{
(...)
}
[ApiExplorerSettings(IgnoreApi=false)]
公共IEnumerable Get(字符串id、int pageIdx、int pageSize)
{
(...)
}
[ApiExplorerSettings(IgnoreApi=false)]
公共HttpResponseMessage Post(MyDto至dto)
{
(...)
}
公共MyApiController(
IContextService(上下文服务)
:base(contextService)
{
}
受保护的覆盖无效处置(布尔处置)
{
如果(处置)
{
(...)
}
基地。处置(处置);
}
}
问题是:
我添加了一个API帮助页面。每当控制器从MyApiControllerBase派生时,POST方法不显示。每当我从ApiController
派生时,一切都很好。另外,当我从MyApiControllerBase导出时,我实际上无法发布任何内容
有人能解释这种行为吗?我做错了什么
请注意,我使用Ninject进行依赖项解析,并使用LightSpeed作为ORM
*从重新发布这感觉像是一个路由问题。由于发送给POST方法的参数类型,可能无法访问该方法。不要将MyDTO用作参数类型,请尝试使用参数的对象类型,然后在方法代码中将对象转换为MyDTO:
public HttpResponseMessage Post(object obj)
{
MyDTO dto = obj as MyDTO;
if (dto != null)
{
...
}
...
}
没有直接关系,但为什么您要用
[ApiExplorerSettings(IgnoreApi=false)]
装饰每个控制器操作?如果确实希望忽略操作/控制器(通过设置IgnoreApi=true
),则只需添加此属性。关于您的帖子的问题,也许您可以添加路由配置。