No response while calling API in ASP.NET C# Web Forms

Web Technologies Web Development 2 years ago

0 1 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating
_x000D_ _x000D_ I have been searching this forum and others to resolve a seemingly simple problem. The objective is to call 2 APIs and make some calculations on the data received. However, when I am calling any API from my code below, there is no response to the PostAsJsonAsync call. Debugging shows that the command is executed but the code after this call is never executed. I do get the following messages in the Chrome developer tool. 'iisexpress.exe' (CLR v4.0.30319: /LM/W3SVC/2/ROOT-2-130904674774978518): Loaded 'C:\Users\ADMIN\AppData\Local\Temp\Temporary ASP.NET Files\vs\6d7d5266\5b17a55b\App_Web_vu1twtot.dll'. Cannot find or open the PDB file. 'iisexpress.exe' (CLR v4.0.30319: /LM/W3SVC/2/ROOT-2-130904674774978518): Loaded 'C:\Users\ADMIN\AppData\Local\Temp\Temporary ASP.NET Files\vs\6d7d5266\5b17a55b\App_Web_h35301am.dll'. The thread 0x3b1c has exited with code 0 (0x0). The thread 0x34a4 has exited with code 0 (0x0). The API being used is a simple fake API from http://jsonplaceholder.typicode.com/. This API and others I have tested respond fine if I use Ajax in JS. Please help me out. The code below is taken from an ASP.NET tutorial at http://www.asp.net/web-api/overview/advanced/calling-a-web-api-from-a-net-client using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Net.Http; using System.Net.Http.Headers; using System.Threading.Tasks; public class Product { public double userid { get; set; } public double id { get; set; } public string title { get; set; } public string body { get; set; } } public partial class _Default : Page { protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { RunAsync().Wait(); } static async Task RunAsync() { using (var client = new HttpClient()) { client.BaseAddress = new Uri("http://jsonplaceholder.typicode.com"); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); // HTTP POST var gizmo = new Product() { userid = 11, id = 100, title = "Widget11", body = "Widget11" }; HttpResponseMessage response = await client.PostAsJsonAsync("/posts/1", gizmo); if (response.IsSuccessStatusCode) { } } } }

Posted on 16 Aug 2022, this text provides information on Web Development related to Web Technologies. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (1)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago
_x000D_ This works properly. HttpResponseMessage response = await client.PostAsJsonAsync("/posts/1", gizmo).ConfigureAwait(false);

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.

Important Web Technologies Links