相关疑难解决方法(0)

ASP.NET Web API中具有多个GET方法的单个控制器

在Web API中,我有一类类似的结构:

public class SomeController : ApiController
{
    [WebGet(UriTemplate = "{itemSource}/Items")]
    public SomeValue GetItems(CustomParam parameter) { ... }

    [WebGet(UriTemplate = "{itemSource}/Items/{parent}")]
    public SomeValue GetChildItems(CustomParam parameter, SomeObject parent) { ... }
}
Run Code Online (Sandbox Code Playgroud)

由于我们可以映射单个方法,因此在正确的位置获得正确的请求非常简单.对于只有一个GET方法但也有Object参数的类似类,我成功使用了IActionValueBinder.但是,在上述情况下,我收到以下错误:

Multiple actions were found that match the request: 

SomeValue GetItems(CustomParam parameter) on type SomeType

SomeValue GetChildItems(CustomParam parameter, SomeObject parent) on type SomeType
Run Code Online (Sandbox Code Playgroud)

我试图通过重写ExecuteAsync方法来解决这个问题ApiController但到目前为止没有运气.关于这个问题的任何建议?

编辑:我忘了提到现在我试图在ASP.NET Web API上移动此代码,它具有不同的路由方法.问题是,如何使代码在ASP.NET Web API上运行?

c# asp.net-web-api

156
推荐指数
9
解决办法
20万
查看次数

ASP.NET WebApi - 一个控制器中的多个GET操作

我有Users控制器和基本的REST模式工作正常.但是我需要一个额外的模式users/{id}/usergroups来返回该用户的所有用户组.

实现这一目标的最佳方法是什么,因为我想我将需要更多控制器上的类似路由.只是默认的还不够......

错误

找到了与请求匹配的多个操作:Api.Models.Users.User GetUser(Int32)类型Api.Controllers.UsersController System.Collections.Generic.IEnumerable`1 [Api.Models.Users.UserGroup] GetUserGroups(Int32)on键入Api.Controllers.UsersController

// GET api/Users
public IEnumerable<User> GetUsers()

// GET api/Users/5
public User GetUser(int id) // THIS IS CONFLICT 1

// PUT api/Users/5
public HttpResponseMessage PutUser(int id, User user)

// POST api/Users
public HttpResponseMessage PostUser(User user)

// DELETE api/Users/5
public HttpResponseMessage DeleteUser(int id)

// GET api/Users/5/UserGroups
public IEnumerable<UserGroup> GetUserGroups(int id)  // THIS IS CONFLICT 2
Run Code Online (Sandbox Code Playgroud)

编辑1

我做了建议的amhed,它没有解决问题.

// GET api/Users/5
[HttpGet, ActionName("getuser")]
public User GetUser(int id) // …
Run Code Online (Sandbox Code Playgroud)

c# asp.net asp.net-mvc-4 asp.net-web-api

18
推荐指数
1
解决办法
2万
查看次数

标签 统计

asp.net-web-api ×2

c# ×2

asp.net ×1

asp.net-mvc-4 ×1