ikey
ikey

Reputation: 331

How to call a Function Task within the same controller using ASP.net MVC?

I'm having a hard time calling a function within the same controller.

This is my function that calls the GetToken Function

 [HttpPost]
        public ActionResult FileLoad()
        {
            using (var reader = new StreamReader("C:\\somedirectory\\Payout.csv"))
            using (var csv = new CsvReader(reader))
            {
                csv.Configuration.RegisterClassMap<FundTransferMap>();
                var json = JsonConvert.SerializeObject(csv.GetRecords<FundTransfer>());
                //Response.Write(json);
                TempData["FileJson"] = json;
                return RedirectToAction("GetToken");
            }
        }

This is the function that should be called by the first function

 [HttpPost]
        private async Task<ActionResult> GetToken()
        {
            var client = new HttpClient();
            var httpRequestMessage = new HttpRequestMessage
            {
                Method = HttpMethod.Post,
                RequestUri = new Uri("https://some-url.com//token"),
                Headers = {
                        //{ HttpRequestHeader.Authorization.ToString(), "Bearer xxxxxxxxxxxxxxxxxxxx" },
                        { HttpRequestHeader.Accept.ToString(), "application/json" },
                        { HttpRequestHeader.ContentType.ToString(), "application/x-www-form-urlencoded"},
                        { "client-id", "client-id"},
                        { "client-secret","client-secret"},
                        { "partner-id","partner-id"},
                        { "X-Version", "1" }

                    },
                Content = new FormUrlEncodedContent(new Dictionary<string, string>
                    {
                        { "client_id", "clientid" },
                        { "grant_type", "password" },
                        { "username", "username" },
                        { "password", "p@ssw0rd" },
                        { "scope", "scope" }
                    })
            };

            var response = client.SendAsync(httpRequestMessage).Result;
            var payload = JObject.Parse(await response.Content.ReadAsStringAsync());
            TempData["accessToken"] = payload.Value<string>("access_token");

        }

        return View();


But this code produces an error on runtime because it's an àsync function I also don't wanted the second function to return something.

Upvotes: 1

Views: 1088

Answers (1)

Daniel
Daniel

Reputation: 9829

I would suggest to rewrite your GetToken() method to return the token as a string.

private async Task<string> GetToken()
{
    var client = new HttpClient();

    // removed code for clarity

    var response = client.SendAsync(httpRequestMessage).Result;
    var payload = JObject.Parse(await response.Content.ReadAsStringAsync());
    var token = payload.Value<string>("access_token");
    return Task.FromResult(token);
}

Then you can easily call this method from any other controller method where you need to get the token:

[HttpPost]
public ActionResult FileLoad()
{
    // removed code for clarity

    // call method GetToken();
    var token = await GetToken();
}

Upvotes: 2

Related Questions