Domanda

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?

È stato utile?

Soluzione

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top