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) { ... }

}

由于我们可以映射各个方法,因此在正确的位置获得正确的请求非常简单。对于只有一个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

我试图通过覆盖ExecuteAsync方法来解决此问题,ApiController但到目前为止还没有运气。关于这个问题有什么建议吗?


编辑:我忘了提一下,现在我正尝试在ASP.NET Web API上移动此代码,而ASP.NET Web API具有不同的路由方法。问题是,如何使代码在ASP.NET Web API上工作?


肥皂起泡泡
浏览 870回答 3
3回答

慕尼黑8549860

从此:config.Routes.MapHttpRoute("API Default", "api/{controller}/{id}",            new { id = RouteParameter.Optional });对此:config.Routes.MapHttpRoute("API Default", "api/{controller}/{action}/{id}",            new { id = RouteParameter.Optional });因此,您现在可以指定要将HTTP请求发送到的操作(方法)。发布到“ http:// localhost:8383 / api / Command / PostCreateUser”会调用:public bool PostCreateUser(CreateUserCommand command){    //* ... *//    return true;}并发布到“ http:// localhost:8383 / api / Command / PostMakeBooking”会调用:public bool PostMakeBooking(MakeBookingCommand command){    //* ... *//    return true;}我在自托管的WEB API服务应用程序中尝试了此方法,它的工作原理很像:)
打开App,查看更多内容
随时随地看视频慕课网APP