ahmed abed elaziz
ahmed abed elaziz

Reputation: 15

How to call action result return json on another action result on asp.net core 2.2?

Problem

How to call Action Result on another Action Result ?

I have two Action Result PostUserLogins and Action Result GetBranches

Can I call ActionResult getbranches inside ActionResult postlogin ?

[HttpPost(Contracts.ApiRoutes.Login.UserLogin)]
        public IActionResult PostUserLogins([FromBody] Users user)
        {
if (LoginStatus == 1)
            {
                // for Invalid Username Or password
                dynamic request_status = new JObject();
                request_status.Status = "failed";
                request_status.Code = LoginStatus;
                request_status.Message = errorMessage;
                request_status.Branches = ????? How to call GetBranches Action;
               // call action result to get  GetBranches(Users user) as json;
                JsonResults = "request_status" + JsonConvert.SerializeObject(request_status);


        }

        }
 [HttpGet(Contracts.ApiRoutes.Login.GetBranches)]
        public IActionResult GetBranches([FromRoute] string UserId)
        {


 List<Branches> branchesList = new List<Branches>();
                for (int i = 0; i < dtBranches.Rows.Count; i++)
                {
                    Branches branch = new Branches();
                    branch.BranchCode = Utilities.ObjectConverter.ConvertToInteger(dtBranches.Rows[i]["BranchCode"]);
                    branch.BranchName = Utilities.ObjectConverter.ConvertToString(dtBranches.Rows[i]["BranchAraName"]);
                    branchesList.Add(branch);

            }
JsonResults = "request_status" + JsonConvert.SerializeObject(branchesList);
return Ok(JsonResults);
        }

Upvotes: 0

Views: 1171

Answers (2)

Ryan
Ryan

Reputation: 20116

If they are in the same controller,you could call it directly in PostUserLogins like:

public IActionResult PostUserLogins([FromBody] Users user)
    {
        //other logic

        var result = GetBranches("myUserID") as OkObjectResult;

        var json = result.Value.ToString().Substring(14);//remove the first "request_status" in the string to make it a valid json  be deserialized later

        request_status.Branches = JsonConvert.DeserializeObject<List<Branch>>(json);//get the Branch list

        JsonResults = "request_status" + JsonConvert.SerializeObject(request_status);

    }

Upvotes: 0

Paulo Morgado
Paulo Morgado

Reputation: 14856

Regardless whether you could or not, you shouldn't.

The simplest way is to extract that logic to another method:

[HttpPost(Contracts.ApiRoutes.Login.UserLogin)]
public IActionResult PostUserLogins([FromBody] Users user)
{
    if (LoginStatus == 1)
    {
        // for Invalid Username Or password
        dynamic request_status = new JObject();
        request_status.Status = "failed";
        request_status.Code = LoginStatus;
        request_status.Message = errorMessage;
        request_status.Branches = GetBrancesImpl();
        JsonResults = "request_status" + JsonConvert.SerializeObject(request_status);
    }
}
[HttpGet(Contracts.ApiRoutes.Login.GetBranches)]
public IActionResult GetBranches([FromRoute] string UserId)
{
    JsonResults = "request_status" + JsonConvert.SerializeObject(GetBrancesImpl());
    return Ok(JsonResults);
}
private IEnumerable<Branches> GetBrancesImpl()
{
    from branch in dtBranches.Rows
    select new new Branches
        {
            BranchCode = Utilities.ObjectConverter.ConvertToInteger(dtBranches.Rows[i]["BranchCode"]),
            BranchName = Utilities.ObjectConverter.ConvertToString(dtBranches.Rows[i]["BranchAraName"]),
        };
}

Best would be to move this logic to a service class that holds the logic and can easily be tested.

Upvotes: 1

Related Questions