Skip to main content
 首页 » 编程设计

c#之使用自定义 WebApi Controller 的问题

2024年01月01日59yyy_WW

我为我的项目创建了一个自定义基础 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 的 Controller :

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); 
    } 
} 

事情是:

我添加了 API 帮助页面。每当 Controller 从 MyApiControllerBase 派生时,POST 方法就不会显示。每当我从 ApiController 派生时,一切都很好。此外,当从 MyApiControllerBase 派生时,我实际上无法发布任何内容。

谁能解释这种行为?我做错了什么?

请注意,我使用 Ninject 来解决我的依赖关系,并使用 LightSpeed 作为我的 ORM。

*转自http://forums.asp.net/t/1855019.aspx/1?Problems+using+custom+ApiControllers

请您参考如下方法:

这感觉像是路由问题。由于发送给它的参数类型,可能无法访问 POST 方法。不要使用 MyDTO 作为参数类型,而是尝试使用对象类型作为参数,然后在方法代码中将对象转换为 MyDTO:

public HttpResponseMessage Post(object obj) 
{ 
    MyDTO dto = obj as MyDTO; 
 
    if (dto != null) 
    {  
      ... 
    } 
 
    ... 
}