亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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

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

C++
嚕嚕噠 2019-12-02 16:04:52
在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 回答

?
一只斗牛犬

TA貢獻1784條經驗 獲得超2個贊

從此:


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-12-03
  • 3 回答
  • 0 關注
  • 475 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號