How to call actionresult on another actionresult ?
I have two ActionResult postlogin and ActionResult GetBranches
Can I call ActionResult getbranches inside ActionResult postlogin ?
[HttpPost(Contracts.ApiRoutes.Login.UserLogin)]
public IActionResult PostUserLogins([FromBody] Users user)
{
// call action result to get GetBranches(Users user) as json;
}
[HttpGet(Contracts.ApiRoutes.Login.GetBranches)]
public IActionResult GetBranches([FromBody] Users user)
{ // here return json
}
This is just a public method you should be able to call as usual. If the problem is dealing with the IActionResult returned value, it could be best to change the IActionResult return type to whatever actual type is expected by the caller.
Still an action is an http endpoint and it could be best to call directly the method that is called from your action (apparently the method that loads a user from your db ?)
[HttpPost(Contracts.ApiRoutes.Login.UserLogin)]
public IActionResult PostUserLogins([FromBody] Users user)
{
// call action result to get GetBranches(Users user) as json;
// And so here it could : // do whatever else you want before
var branches=MyService.GetBranches(user.Id); // call your underlying "building block"
// do whatever else needs to be done
}
[HttpGet(Contracts.ApiRoutes.Login.GetBranches)]
public IActionResult GetBranches([FromBody] Users user)
{
return MyService.GetBranches(user.Id); // Not sure why you have a full user object on what appears to be a GET ??
}
In short action are just "http entry points" you need to expose and behind the scene you just call whatever is doing the actual work.
If you have already complex code right into GetBranches and you need to call this it seems a bad application architecture. A first step miight at least to turn this into a private method you could call when needed elsewhere in your controller.
Member
39 Points
362 Posts
How to call actionresult on another actionresult on asp.net core 2.2 ?
Sep 11, 2019 11:14 AM|ahmedbarbary|LINK
Problem
How to call actionresult on another actionresult ?
I have two ActionResult postlogin and ActionResult GetBranches
Can I call ActionResult getbranches inside ActionResult postlogin ?
All-Star
53091 Points
23659 Posts
Re: How to call actionresult on another actionresult on asp.net core 2.2 ?
Sep 11, 2019 11:24 AM|mgebhard|LINK
Simply invoke the method.
A couple of observations, [FromBody] is invalid in a GET as there's no body.
[HttpGet(Contracts.ApiRoutes.Login.GetBranches)] public IActionResult GetBranches([FromBody] Users user) { // here return json }
The approach indicates design issues. Refactor the shared code into a class or helper methods.
All-Star
48570 Points
18082 Posts
Re: How to call actionresult on another actionresult on asp.net core 2.2 ?
Sep 11, 2019 11:33 AM|PatriceSc|LINK
Hi,
This is just a public method you should be able to call as usual. If the problem is dealing with the IActionResult returned value, it could be best to change the IActionResult return type to whatever actual type is expected by the caller.
Still an action is an http endpoint and it could be best to call directly the method that is called from your action (apparently the method that loads a user from your db ?)
Member
39 Points
362 Posts
Re: How to call actionresult on another actionresult on asp.net core 2.2 ?
Sep 11, 2019 12:37 PM|ahmedbarbary|LINK
can you show me how to use iactionresult to get recieved result from action result without using public method
All-Star
48570 Points
18082 Posts
Re: How to call actionresult on another actionresult on asp.net core 2.2 ?
Sep 11, 2019 01:46 PM|PatriceSc|LINK
I would expect something such as :
In short action are just "http entry points" you need to expose and behind the scene you just call whatever is doing the actual work.
If you have already complex code right into GetBranches and you need to call this it seems a bad application architecture. A first step miight at least to turn this into a private method you could call when needed elsewhere in your controller.