使用自定义Web Api控制器的问题

时间:2012-10-30 14:27:35

标签: 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 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的控制器:

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

重新发布

1 个答案:

答案 0 :(得分:0)

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

public HttpResponseMessage Post(object obj)
{
    MyDTO dto = obj as MyDTO;

    if (dto != null)
    { 
      ...
    }

    ...
}