Search code examples
c#asp.net-mvcapidnx50

calling action method from another action


I have a put method in controller A where i pass in json object and do some checks in dynamo db, based on my output

[HttpPut]
public async Task<IActionResult> ProcessEmployee([FromBody]EmployeeModel em) 

i need to do a post in controller B

[HttpPost]
public async Task<IActionResult> CreateEmployee([FromBody]EmployeeModel em)

or

do a put in controller B

[HttpPut]
public async Task<IActionResult> UpdateEmployee([FromBody]EmployeeModel em) 

how do i redirect to the actions in controller B and also pass in my json object which i pass to the put in controller A.

Thanks for reading.


Solution

  • It is not a best practice for a controller to redirect calls to other controllers.
    If you want to create/update employee record in your database after the JSON object has been processed by controller A, you may want to implement DAL (Data Access Layer) for db operations, that will be called from controller A.
    The DAL interface should be very straightforward:

    void CreateEmployee(EmployeeModel em);
    void UpdateEmployee(EmployeeModel em);