Search code examples
c#asp.net-mvcasp.net-web-apiasp.net-web-api-routing

How to use multiple GET action in a WebApi?


Well I am actually trying to learn the WebApi and suppose if i have a scenario where i have two get methods like this

CONTROLLER

public class EmployeeApiController : ApiController
{
    public List<Student> GetAllStudents() { ... }

    public List<Student> EmailChange(string studentName, string Email) { ... }

    public List<Student> AddressChange(string studentName, string Address) { ... }
}

public class Student
{
    public string StudentName { get; set; }
    public string Address { get; set; }
    public string Email { get; set; }
    public static List<Student> students { get; set; }
}

I am not able to call the respective method, how can i do that, i know there are plenty of blogs but it has not helped me to understand how to really access the methods. by going through several blogs i made my entire code like this

WebApiConfig Code

public static void Register(HttpConfiguration config)
{
    config.MapHttpAttributeRoutes();

    config.Routes.MapHttpRoute(
        name: "DefaultApi",
        routeTemplate: "api/{controller}/{action}/{id}",
        defaults: new { id = RouteParameter.Optional }
    );
}

Controller Code

    public List<Student> GetAllStudents()
    {
        Student.students = new List<Student> {
            new Student { StudentName="foo",Address="usa",Email="foo@yahoo.com"},
            new Student { StudentName="joe",Address="mumbai",Email="joe@yahoo.com"},
            new Student { StudentName="albert",Address="georgia",Email="albert@yahoo.com"},
            new Student { StudentName="prince",Address="missisipi",Email="prince@yahoo.com"}
        };
        return Student.students;
    }
    [HttpGet]
    public List<Student> UpdateEmail(string studentName, string Email)
    {
        return Student.students.Select(i =>
        {
            if (i.StudentName == studentName)
            {
                i.Email = Email;
            }
            return i;
        }).ToList();

    }
    [HttpGet]
    public List<Student> UpdateAddress(string studentName, string Address)
    {
        return Student.students.Select(x =>
        {
            if (x.StudentName == studentName)
            {
                x.Address = Address;
            }
            return x;
        }).ToList();
    }
}

public class Student
{
    public string StudentName { get; set; }
    public string Address { get; set; }
    public string Email { get; set; }
    public static List<Student> students { get; set; }
}

I am little confused on how to access both the UpdateEmail method and the UpdateAddress method using the GET request.

UPDATE 1

When i make a call like this

http://localhost:53711/api/EmployeeApi/UpdateAddress or

http://localhost:53711/api/EmployeeApi/UpdateEmail

i get an error like this

enter image description here

and when i make a call like this i get an error like

http://localhost:53711/api/EmployeeApi/UpdateEmail/foo/foo enter image description here


Solution

  • Either change template to routeTemplate: "api/{controller}/{action}/{studentName}", and leave methods as it

    public static void Register(HttpConfiguration config) {
        config.MapHttpAttributeRoutes();
    
        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{action}/{studentName}"
        );
    }
    

    OR

    leave template as is, ie: "api/{controller}/{action}/{id}" and change method parameter to (string id,.......)

    [HttpGet]
    public List<Student> UpdateEmail(string id, string Email) { ... }
    

    OR

    You could also forego convention-based routing and use attribute routing

    [RoutePrefix("api/EmployeeApi")]
    public class EmployeeApiController : ApiController
    {
        //GET api/EmployeeApi
        [HttpGet]
        [Route("")]
        public List<Student> GetAllStudents() { ... }
    
        //GET api/EmployeeApi/EmailChange/foo/foo@email.com
        [HttpGet]
        [Route("EmailChange/{studentName}/{email}")]
        public List<Student> EmailChange(string studentName, string email) { ... }
    
        //GET api/EmployeeApi/AddressChange/foo/China
        [HttpGet]
        [Route("AddressChange/{studentName}/{address}")]
        public List<Student> AddressChange(string studentName, string Address) { ... }
    }