- I have a Web Forms application calling a Web API post action but it is not working.
- Calling the Post action from Swagger and Postman is working. So am thinking the issue is not in the Web API.
- Have tried using the PostAsJsonAsync function but the outcome is the same.
- The execution hangs at the line "HttpResponseMessage response = await client.PostAsync("api/create", RawText);"
- Kindly assist
Web Forms Method
public async Task<CSSetup> CSCreateSetup(CSAudit TempAudit)
{
using (var client = new HttpClient())
{
var APIPath = Session["APIPath"].ToString();
CSSetup TempSetup = new CSSetup();
//Content
var json = JsonConvert.SerializeObject(TempAudit);
var RawText = new StringContent(json, UnicodeEncoding.UTF8, "application/json");
//HttpClient
client.BaseAddress = new Uri(APIPath);
HttpResponseMessage response = await client.PostAsync("api/create", RawText);
if (response.IsSuccessStatusCode)
{
TempSetup = await response.Content.ReadAsAsync<CSSetup>();
}
else
{
Session["Message"] = "Error creating setup";
}
return await Task.FromResult(TempSetup);
}
}
Post Action
[HttpPost]
[Route("Create")]
public async Task<ActionResult> CreateSetup(CSAudit TempAudit)
{
try
{
return Ok(await setupManager.CSCreateSetup(TempAudit));
}
catch (Exception)
{
return StatusCode(StatusCodes.Status500InternalServerError, "Error creating setup object");
}
}
Calling CreateSetup
protected async void BtnSave_Click(object sender, EventArgs e)
{
var TempAudit = new CSAudit("ADMIN", DateTime.Now.ToString("yyyyMMdd"));
CSSetup TempSetup = (CSCreateSetup(TempAudit)).Result;
}
What I have tried
- Created the Web API Post action and the Web Form method to consume the Post action
- I was expecting the Web Forms data to Post the data supplied and return the expected data
I have converted the call to include Result and removed the await. Have also removed the async key word from the CreateSetup method.
Old Code
New Code