如何调用动作结果在asp.net core 2.2上的另一个动作结果上返回json?

时间:2019-09-11 12:30:00

标签: asp.net-mvc-4 asp.net-core c#-7.0

问题

如何在另一个操作结果上调用操作结果?

我有两个操作结果PostUserLogins和操作结果GetBranches

我可以在ActionResult postlogin内调用ActionResult获取分支吗?

[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);
        }

2 个答案:

答案 0 :(得分:1)

无论是否可以,都不应该。

最简单的方法是将该逻辑提取到另一种方法:

[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"]),
        };
}

最好是将此逻辑移到包含该逻辑并且可以轻松测试的服务类中。

答案 1 :(得分:0)

如果它们在同一控制器中,则可以直接在PostUserLogins中调用它,例如:

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

    }