为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

C#
肥皂起泡泡 2019-10-23 14:32:02
在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 SomeTypeSomeValue GetChildItems(CustomParam parameter, SomeObject parent) on type SomeType我试图通过覆盖ExecuteAsync方法来解决此问题,ApiController但到目前为止还没有运气。关于这个问题有什么建议吗?编辑:我忘了提一下,现在我正尝试在ASP.NET Web API上移动此代码,而ASP.NET Web API具有不同的路由方法。问题是,如何使代码在ASP.NET Web API上工作?
查看完整描述

3 回答

?
慕尼黑8549860

TA贡献1818条经验 获得超11个赞

从此:


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服务应用程序中尝试了此方法,它的工作原理很像:)


查看完整回答
反对 回复 2019-10-23
  • 3 回答
  • 0 关注
  • 867 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信