Sujeet Raman

Sujeet Raman

  • 862
  • 927
  • 351.8k

How to call one controller from another controller in asp.net core api

Jan 19 2021 8:06 AM
Hi, this description may weired! but as a POC I want to do something like ..
 
I have an Employee controller .this controller is concidered as main controller and having a methode which fetch all the employee details based on their grade.Now i have to create 2 microservices for my project.Age and Gender is the 2 seperate MS.I want to know the correct way of creating/implimenting these microservices.
 
In Employee controller
  1. [HttpPost("api/Employee/GetallEmployee")]  
  2. public async Task GetallEmployee(input)  
  3. {  
  4. //i have to get merged json result here from below controllers(2 microservices having seperate db and seperate controller) and this url will connect with API gateway  
  5. // I should merge these 2 result( using newtonsoft i can do it)  
  6. //the merged json responce will concidered as main api  
  7. //is this the correct way to create microservices?  
  8. mergedresult= result1+result2  
  9. }
But in this i have some microservices like Age and Gender means for creating microservices in to my solution i have created seperate 2 controller like AgeController and GenderController
 
But the challenge i am facing is i have to call the main api only api/Employee/GetallEmployee
by using this i have to call both below controller and in main methode i shud combain the 2 joson results.Anyone please help me
 
AgeController
  1. [HttpPost("api/Age/Ageofall")]  
  2. public async Task Ageofall(input)  
  3. {  
  4. var result1 = await _getemployee.getalldetails(input);  
  5. return new JsonResult(result1);  
  6. }  
  7. GenderController  
  8. [HttpPost("api/Gender/Genderofall")]  
  9. public async Task Genderofall(input)  
  10. {  
  11. var result2 = await _getemployee.getalldetails(input);  
  12. return new JsonResult(result2);  
  13. }
Please suggest the best practice to create microservices if i am doing wrong here..Thanks

Answers (2)