0
私は比較的新しいaspとc#です。私の非同期アクションメソッドの呼び出し方法(または場所)
私は外部APIを呼び出し、HttpResponseMessageを返す次の非同期アクションメソッドを持っています。実行時にIndex()アクションメソッドが呼び出されたときに実行します。 IveはIndex()内から呼び出すことを試みましたが、Index()も非同期でなければならないと言われていますが、非同期にする方法や多分、間違っている方法はわかりません。
async static void PostRequest(string url, string userID, string accessKey, string djID, string artistName, string artistURL)
{
IEnumerable<KeyValuePair<string, string>> queries = new List<KeyValuePair<string, string>>()
{
new KeyValuePair<string, string>("UserID", userID),
new KeyValuePair<string, string>("AccessKey", accessKey),
new KeyValuePair<string, string>("DJID", djID),
new KeyValuePair<string, string>("ArtistName", artistName),
new KeyValuePair<string, string>("URL", artistURL)
};
HttpContent q = new FormUrlEncodedContent(queries);
using (HttpClient client = new HttpClient())
{
using (HttpResponseMessage response = await client.PostAsync(url, q))
{
using (HttpContent content = response.Content)
{
string myContent = await content.ReadAsStringAsync();
HttpContentHeaders headers = content.Headers;
System.Diagnostics.Debug.WriteLine(myContent);
}
}
}
}
これを確認してくださいhttps://docs.microsoft.com/en-us/aspnet/mvc/overview/performance/using-asynchronous-methods-in-aspnet-mvc-4 – hardkoded