0

I have web api 2.0 project When i try to implement several method with same parameter, i have this error : Several actions found

namespace WebApi.Controllers
{
    public class EventController : ApiController
    {
        [HttpGet]
        public HttpResponseMessage GetTags(string token, int messageId)
        {
            return ApiCall<List<EntityTag>>.CallApi(token, ServicesMessage.GetTags(messageId));
        }

        [HttpGet]
        public HttpResponseMessage Get(string token, int eventId)
        {
            return ApiCall<EntityEvent>.CallApi(token, ServicesEvent.Get(eventId));
        }
    }
}

Any idea?

Thanks

Alessio Cantarella
  • 5,077
  • 3
  • 27
  • 34

2 Answers2

1

The WebAPI cannot distinquish between these two methods because they have the same parameter types and same HttpVerb, and use implicit routing (no route attribute on them).

I'm a big fan of explicit routing for controllers and methods, instead of depending on naming conventions - so I'd try adding a route attribute to the methods:

public class EventController : ApiController
{
    [HttpGet]
    [Route("gettags")]
    public HttpResponseMessage GetTags(string token, int messageId)
    {
        return ApiCall<List<EntityTag>>.CallApi(token, ServicesMessage.GetTags(messageId));
    }

    [HttpGet]
    [Route("get")]
    public HttpResponseMessage Get(string token, int eventId)
    {
        return ApiCall<EntityEvent>.CallApi(token, ServicesEvent.Get(eventId));
    }
}

... and add a RoutePrefix attribute on the controller itself, like so:

[RoutePrefix("/api/Event")]
public class EventController : ApiController
{
}

Then you should be able to call the methods with a GET request to these URLs:

/api/event/get?token=xxxxx&eventId=xxxx

and

/api/event/gettags?token=xxxxx&messageId=xxxx
HaukurHaf
  • 13,522
  • 5
  • 44
  • 59
0

Write this line in your webapiconfig.cs

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

your Controller:

namespace WebApi.Controllers
{
    public class EventController : ApiController
    {
        [HttpGet]
        public HttpResponseMessage Tags(string token, int messageId)
        {
            return ApiCall<List<EntityTag>>.CallApi(token, ServicesMessage.GetTags(messageId));
        }

        [HttpGet]
        public HttpResponseMessage Events(string token, int eventId)
        {
            return ApiCall<EntityEvent>.CallApi(token, ServicesEvent.Get(eventId));
        }
    }
}

Using this you can give the action names for every get request.

POST

when you want same things with post method just write below line in weapiconfig.cs

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

And after that you can give a action name. no need to routing after using this.

When you want to call api than write follows:

api/Event/Tags //parameter as per your requirement
api/Event/Events //parameter as per your requirement
Kiran Joshi
  • 1,758
  • 2
  • 11
  • 34