none
How can you have multiple Get methods in a RESTful setup? RRS feed

  • Question

  • Hi all;

    How can you setup multiple Get methods when dealing with REST?

    I already have methods like:

    1) List<item> Get();

    2) item Get(Guid id);

    3) void Post(Guid id, [from body] item a);

    4) HttpResponseMessage Put(Guid id, [from body] item a);

    Now I would like to have a get like this...

    List<item2> Get(Guid id);

    This would be a different method than the one listed before but when I compile this(C#) it tells me that it can't distinguish between 2 and the new one , even though the output parameters are different. Is there a way around this?


    Shawn

    Tuesday, August 6, 2013 2:37 AM

Answers

All replies

  • Hi,

    If you wang to setup multiple Get methods when dealing with REST, please try to specify the HTTP method with an attribute: AcceptVerbs, HttpDelete, HttpGet, HttpHead, HttpOptions, HttpPatch, HttpPost, HttpPut. Otherwise, if the name of the controller method starts with "Get", "Post", "Put", "Delete", "Head", "Options", or "Patch", then by convention the action supports that HTTP method. If none of the above, the method supports POST.

    For more information, please try to refer to the following articles:

    http://www.asp.net/web-api/overview/web-api-routing-and-actions/routing-in-aspnet-web-api .

    http://www.asp.net/web-api/overview/web-api-routing-and-actions/routing-and-action-selection .

    Best Regards.


    Amy Peng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    Tuesday, August 6, 2013 8:43 AM
    Moderator
  • Take a look at the code below where in a  webapi controller have multiple Get methods having different parameters and the corresponding route.config file to handle these Get methods..

    Hope this helps you..

    namespace RAISWebApi.Controllers
    {
        public class EmployeeController : ApiController
        {
            //Use this [HttpGet] attribute for all the Get methods
            //Make sure that parameters name are matching with the parameters you are passing from client
            [HttpGet]
            public IEnumerable<Employee> GetEmployee(string ID)
            {           
                return employeeList;
            }
     
            [HttpGet]
            public IEnumerable<Employee> GetEmployeePhone(string ID, string EmpName)
            {
                return employeeList;
            }
     
            [HttpGet]
            public IEnumerable<Employee> GetEmployeeAddress(string ID, string EmpName, string EmpEMail)
            {
                return employeeList;
            }
     
        }
    }

    And you can have route config something like this

    public static void RegisterRoutes(RouteCollection routes)
           {
               routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
     
               routes.Clear();
     
               routes.MapHttpRoute(
                   name: "ActionApi",
                   routeTemplate: "api/{controller}/{action}/{id}",
                   defaults: new { id = RouteParameter.Optional }
               );
     
               routes.MapHttpRoute(
                   name: "DefaultApi",
                   routeTemplate: "api/{controller}/{id}",
                   defaults: new { id = RouteParameter.Optional }
               );
     
               routes.MapRoute(
                   name: "Default",
                   url: "{controller}/{action}/{id}",
                   defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
               );
     
     
           }

    Wednesday, October 19, 2016 2:13 PM