Home > OS >  ActionResult API handle
ActionResult API handle

Time:07-29

I have this WebAssembly project app, alongside a Net Core app that has an api (restful generic) and the api can return NotFound();, and that is recieved in

private async Task RetrieveData()
{
    var dataTemp = await Client.GetFromJsonAsync<Drivers>($"api/Drivers/2");
    if (dataTemp != null)
    {
        text = dataTemp.name;
    }
}

Is there a way to handle ActionResults lik "ok" or "NotFound" with code?, I can't seem to find any information about that other than people using Postman to visualize the actionresult code.

If it's posible, is it intended or it's just bad practice and the purpose of ActionResults is for developers to see what's happening?

CodePudding user response:

You need to modify your code a little bit like this.

private async Task RetrieveData()
{
    var response = await Client.GetAsync($"api/Drivers/2");
    if(response.IsSuccessStatusCode)
    {
        dataTemp = response.Content.ReadFromJsonAsync<Drivers>();
    }
    else
    {
        //Handle exception.
    }
}
  • Related