Web API 2 Controller with multiple get methods

I have two other posts on multiple GET methods, one for ASP.NET 5 Web Api, and another for Web Api 2.

It seems that every version of Web API changes how it handles default routes or route prefixes or whatever the next name will be.

I’ve written previously on how to handle multiple get methods in Asp.Net 5 Web API.

It’s a little different in Web Api 2.

using System.Web.Http;

namespace WebApiMultipleGets.Controllers
{
    [RoutePrefix("api/values")]
    public class ValuesController : ApiController
    {
        public string Get()
        {
            return "simple get";
        }

        [Route("geta")]
        public string GetA()
        {
            return "A";
        }

        [Route("getb")]
        public string GetB()
        {
            return "B";
        }

        [Route("getc")]
        public string GetC()
        {
            return "C";
        }

        [Route("getd")]
        public string GetD()
        {
            return "D";
        }
    }
}

3 thoughts on “Web API 2 Controller with multiple get methods

  1. Pingback: Web API 2 Controller with multiple GET methods – part 2 | no dogma blog

  2. Nice ..this helped me lot. Here is my code with multiple parameters which may help some one :

    [RoutePrefix(“api/File”)]
    public class FileController : ApiController
    {
    [Route(“getfile”)]
    public HttpResponseMessage GetFile(string docid, string sourceurl)
    {
    //do
    return “string”;
    }

    [HttpGet]
    [Route(“savefile”)]
    public string SaveFile(string docid, string sourceurl, string destinationurl)
    {
    //do
    return “string”;
    }

    Calling like this
    http://localhost:12345/api/file/getfile?docid=1234&sourceurl=http://pcm.com/testpcm/

    and

    http://localhost:57916/api/file/savefile?docid=2&sourceurl=http://testing.com&destinationurl=http://destination.com

    No need to change anything in routeconfig/webapiconfig.

    -PURNA

  3. Pingback: ASP.NET 5 Web Api Controller with multiple get methods | no dogma blog

Leave a Reply

Your email address will not be published. Required fields are marked *

− 3 = three