Question

Suppose I am having POST method for Student and User. Since as limit to object definition is both having properties with same names. like

Student : First Name ,Last Name,Address.
Employee: First Name ,Last Name,Address.

and say my POST method is like as :

 public void Post([FromBody]Student _student)
        {//my code
        }

and another is same in same class:

 public void Post([FromBody]Employee _employee)
        {//my code
        }

When accessing this method from external device say from Android, It will be the JSon object. formatted with same parameter and values. how rout will identify which method to execute?

Was it helpful?

Solution

You can define a separate route for each method like the following code:

public class ContactsController : ApiController

{

    [Route("api/Contacts/Students")]
    public void Post([FromBody] Student student)
    {

    }

    [Route("api/Contacts/Employees")]
    public void Post([FromBody] Employee employee)
    {

    }

}

Otherwise, Web API will throw an exception owing to ambiguity.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top