dcsimg
Results 1 to 2 of 2

Thread: How to call action from another action on same controller on web api asp.net core 2.2

  1. #1

    Thread Starter
    Member
    Join Date
    Apr 2018
    Posts
    63

    How to call action from another action on same controller on web api asp.net core 2.2

    problem
    How to call action from another action on same controller on web api asp.net core 2.2 ?
    meaning i have action result do login as post
    I need when success login generate access token
    but generate access token made in another action
    How to call another action generate access token ?
    I need the way for calling action generatetoken from action postlogin
    Code:
       [HttpPost(Contracts.ApiRoutes.Login.UserLogin)]
            public IActionResult PostUserLogins([FromBody] Users user)
            {
         
                string JsonResults = "";
                int LoginStatus = _SecurityService.PostUserLogin(user.UserName, user.Password, out DataTable dtBranches, out string errorMessage);
                if (LoginStatus == 0)
                {
                    dynamic request_status = new JObject();
                    request_status.Status = "succeeded";
                    request_status.Status = 0;
                    request_status.Message = errorMessage;
                  // get list of Branches 
                    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);
                        
                    }
             
                    dynamic user_data = new JObject();
                    user_data.UserName = user.UserName;
                    user_data.LoginTime = DateTime.Now.ToString();
                    //user_data.Branches = new JArray(branchesList);
                    user_data.Branches = JToken.FromObject(branchesList);
    
    
                    dynamic AccessToken = _SecurityService.GenerateTokens(user.UserName, out object jwtcontent);
               
                    dynamic ResultSuccessLogin = new JObject();
                    ResultSuccessLogin.request_status = request_status;
                 
                    ResultSuccessLogin.user_data = user_data;
                
    User.Accesstoken = //call action GenerateAccessToken here to get result of json here 
    
                    JsonResults = JsonConvert.SerializeObject(ResultSuccessLogin);
    
                  
                }
     [HttpGet(Contracts.ApiRoutes.Access.AccessToken)]
            public JsonResult GenerateAccessToken(string userid)
            {
            }
    Last edited by engbarbary; Sep 7th, 2019 at 11:54 PM. Reason: more details

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,733

    Re: How to call action from another action on same controller on web api asp.net core

    An action is a method like any other, so you can call it it like any other method if that is appropriate. Of course, it may be more appropriate to put the common functionality into a private method and then call that from both actions.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width