0
votes

I m new to WebAPI and just exploring its default sample "value" controller which is there out of box with project.

I see it was already having two Get methods:

      // GET api/values
    public IEnumerable<string> Get()
    {
        return new string[] { "value1", "value2" };
    }

    // GET api/values/5
    public string Get(int id)
    {
        return "value";
    }

I tried and changed int id with a complex type and received "Multiple actions were found that match the request"

Why is that it was working fine beofre ?

My route is defuatl:

 routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );

I m passing a complex object in body using Get methoed, I know it is not Restful way but please help me understand it.

Much appreciated.

1
why would you pass a complex object to a GET method? what are you trying to do?Nikki9696
You can, but you'd then need to map what that object is, such as Get (ObjectType thing)Nikki9696
because GET method reads data from the querystring of the request url. There is a limitation on how much data you can send via querystring. On the other hand, POST requests read data from the request body in which you can send anythingShyju
Why are you not using a HttpPost method ? They were created for this use case.Shyju
Kinda a big topic, but here's a few things about how routing works, which is the basics of your question asp.net/web-api/overview/web-api-routing-and-actionsNikki9696

1 Answers

1
votes

You can use ActionName annotation for this issue. For example use:

 [ActionName("IEnumerableGet")]
 public IEnumerable<string> Get()
    {
        return new string[] { "value1", "value2" };
    }

So you can call IEnumerableGet to have this method get called.