locked
How to call actionresult on another actionresult on asp.net core 2.2 ? RRS feed

  • Question

  • User696604810 posted

    Problem

    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 }

    Wednesday, September 11, 2019 11:14 AM

All replies

  • User475983607 posted

    Simply invoke the method.

    [HttpPost(Contracts.ApiRoutes.Login.UserLogin)]
    public IActionResult PostUserLogins([FromBody] Users user)
    {
    	return GetBranches(user);
    }
    [HttpGet(Contracts.ApiRoutes.Login.GetBranches)]
    public IActionResult GetBranches(Users user)
    {
    	// here return json 
    }

    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.

    Wednesday, September 11, 2019 11:24 AM
  • User753101303 posted

    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 ?)

    Wednesday, September 11, 2019 11:33 AM
  • User696604810 posted

    can you show me how to use iactionresult to get recieved result from action result without using public method

    Wednesday, September 11, 2019 12:37 PM
  • User753101303 posted

    I would expect something such as :

    [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.

    Wednesday, September 11, 2019 1:46 PM