无法正确调用多个get方法

时间:2018-06-02 06:42:27

标签: asp.net-web-api

我在Web api AgentApiController中有以下获取方法

    public IHttpActionResult Get()      
    public IHttpActionResult Get(string agentid)                
    public TennetDetails getTenantDetail(string tenantid)        
    public IHttpActionResult getAgentByNumber(string mobile)        
    private Agent GetAgentDetail(PartyQuery query)        
    public IHttpActionResult GetDigiAgentDetail(int agentid)        
    public IHttpActionResult GetDigiAgentHistory(int agentid)

当我致电AgentApi / Get / 7时,public IHttpActionResult Get()会被触发。实际上,我需要触发IHttpActionResult Get(string agentid)

当我删除public IHttpActionResult Get()方法并致电public IHttpActionResult Get()然后 public IHttpActionResult GetDigiAgentHistory(int agentid)开火

我已将WebApiConfig中的tyhe路由更改为

`

      config.Routes.MapHttpRoute("DefaultApiWithId", "Api/{controller}/{id}", new { id = RouteParameter.Optional }, new { id = @"\d+" });

        config.Routes.MapHttpRoute("DefaultApiWithAction", "Api/{controller}/{action}");

        config.Routes.MapHttpRoute("DefaultApiGet", "Api/{controller}", new { action = "Get" }, new { httpMethod = new HttpMethodConstraint(HttpMethod.Get) });

        config.Routes.MapHttpRoute("DefaultApiPost", "Api/{controller}", new { action = "Post" }, new { httpMethod = new HttpMethodConstraint(HttpMethod.Post) });

        config.Routes.MapHttpRoute("DefaultApiWithActionWithId", "api/{controller}/{action}/{id}", new { id = RouteParameter.Optional });

`

但我仍无法找到问题的解决方案

1 个答案:

答案 0 :(得分:0)

在我的代码中存在一些问题

我改变了

public IHttpActionResult Get(string agentid)

public IHttpActionResult Get(int id)

现在代码工作正常。

我想因为我在路线中提到了id(参数名称),

config.Routes.MapHttpRoute("DefaultApiWithId", "Api/{controller}/{id}", new { id = RouteParameter.Optional }, new { id = @"\d+" });

我应该使用id本身作为动作参数的名称。