RSS

Multiple GET methods in ASP.NET Web API

21 May

The default  controller web api gives you has the following pattern The built in GET,POST,PUT convention that web API.supports .I have found to support extra GET methods and support the normal REST methods as well.

public class MyController : ApiController
    {
        // GET api/test
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }
 
        // GET api/test/5
        public string Get(int id)
        {
            return "value";
        }
 
        // POST api/test
        public void Post([FromBody]string value)
        {
        }
 
        // PUT api/test/5
        public void Put(int id, [FromBody]string value)
        {
        }
 
        // DELETE api/test/5
        public void Delete(int id)
        {
        }
    }

the Problems start  when you want to define more than one  “GET” method that goes by a different name.which always get one method & give Error.

  public class MyController : ApiController
    {
        // GET api/test
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }
 
        // GET api/test/GetAllWithFilter
        [HttpGet]
        public IEnumerable<string> GetAllWithFilter()
        {
            return new string[] { "value1"};
        }
 
        // GET api/test/5
        public string Get(int id)
        {
            return "value";
        }
 
        // POST api/test
        public void Post([FromBody]string value)
        {
        }
 
        // PUT api/test/5
        public void Put(int id, [FromBody]string value)
        {
        }
 
        // DELETE api/test/5
        public void Delete(int id)
        {
        }
    }

This shouldn’t be that hard to fix on the surface but the built in route that enables this convention.I searched around for a while to figure out how to solve this then finally ran across a great Stackoverflow post here that nails it and http://lonetechie.com/2013/03/04/fixing-multiple-actions-were-found-that-match-the-request-aspnet-webapi/.

Replace your built in route in WebApiConfig with this (updated for latest release).

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

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

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

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

 config.Routes.MapHttpRoute("DefaultApiPut", "api/v1/{controller}", 
new{ action ="Put"}, new{ httpMethod =new HttpMethodConstraint(HttpMethod.Put)}); 

config.Routes.MapHttpRoute("DefaultApiDelete", "api/v1/{controller}", 
new{ action ="Delete"}, new{ httpMethod =new HttpMethodConstraint(HttpMethod.Delete)});

 


Advertisements
 
Leave a comment

Posted by on May 21, 2014 in MVC, Web API

 

Tags: ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: