Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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="[email protected]"},
            new Student { StudentName="joe",Address="mumbai",Email="[email protected]"},
            new Student { StudentName="albert",Address="georgia",Email="[email protected]"},
            new Student { StudentName="prince",Address="missisipi",Email="[email protected]"}
        };
        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

like image 650
Lijin Durairaj Avatar asked Jan 08 '17 17:01

Lijin Durairaj


People also ask

Can we have multiple get methods in Web API?

As mentioned, Web API controller can include multiple Get methods with different parameters and types. Let's add following action methods in StudentController to demonstrate how Web API handles multiple HTTP GET requests.

How do I use multiple posts in Web API?

Open your controller class, in our project its ValuesController. cs >> Copy paste below code, these are two sample post methods with a string input and return parameter – you can write your business logic in it. Similarly, you can add any number of POST, GET, PUT, DELETE methods in one controller.


2 Answers

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/[email protected]
    [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) { ... }
}
like image 66
Nkosi Avatar answered Sep 21 '22 13:09

Nkosi


your WebApConig.cs should look like the following -

api/{controller}/{action}/{id}

then issue calls like -

http://localhost:port/api/Ctrl/action
like image 36
Amit Kumar Ghosh Avatar answered Sep 21 '22 13:09

Amit Kumar Ghosh