Web Api 已经定义了一个名为“Get”的成员,具有相同的参数类型

Gav*_*llo 2 c# asp.net asp.net-mvc asp.net-web-api

public class UsersController : ApiController
{
    // GET api/companies/{company_id}/users/{user_id}
    public object Get(int company_id, int user_id)
    {
       ...
    }

    // GET api/companies/{company_id}/plants/{plant_id}/users
    public IEnumerable<object> Get(int company_id, int plant_id)
    {
       ...
    }
}
Run Code Online (Sandbox Code Playgroud)

第一种方法应该返回公司的单个用户部分第二种方法应该返回公司和工厂的列表用户部分

错误类型 '...' 已经定义了一个名为 'Get' 的成员,具有相同的参数类型

是否可以避免 Web Api 将第二个 Get 解释为重新定义?

如果查看http://aspnetwebstack.codeplex.com/wikipage?title=Attribute%20routing%20in%20Web%20API 在场景 3 中使用 [HttpGet("...")] 符号

public class MoviesController : ApiController
{
    [HttpGet("actors/{actorId}/movies")]
    public Movie Get(int actorId) { }
    [HttpGet("directors/{directorId}/movies")]
    public Movie Get(int directorId) { }
}
Run Code Online (Sandbox Code Playgroud)

但在我的情况下 [HttpGet(...)] 错误'System.Web.Http.HttpGetAttribute' 不包含带 1 个参数的构造函数

nim*_*ima 6

我认为你必须命名你的 get 函数:

public class UsersController : ApiController
{
    // GET api/companies/GetByUser/{company_id}/{user_id}
    public object GetByUser(int company_id, int user_id)
    {
       ...
    }

    // GET api/companies/GetByPlanet/{company_id}/{plant_id}
    public IEnumerable<object> GetByPlanet(int company_id, int plant_id)
    {
       ...
    }
}
Run Code Online (Sandbox Code Playgroud)