i've an blazor component with the following code:
@using System.Net
@inject IConfiguration conf
@inject HttpClient Http
@inject NavigationManager navMgr
@{
checkBackend();
}
@code {
private async void checkBackend() {
try {
var response = await Http.GetAsync(conf.GetSection("AppSettings:ApiHost").Value + "/ping");
if(response==null) {
throw new ApplicationException("API-Host unreachable..");
}
if (!response.IsSuccessStatusCode)
{
var statusCode = response.StatusCode;
var content = await response.Content.ReadAsStringAsync();
switch (statusCode)
{
case HttpStatusCode.NotFound: navMgr.NavigateTo("/Error/404");
break;
case HttpStatusCode.Unauthorized: navMgr.NavigateTo("/Error/Unauthorized");
break;
default: navMgr.NavigateTo("/Error/500");
break;
}
throw new ApplicationException($"Reason: {response.ReasonPhrase}"); //or add a custom logic here
}
} catch(HttpRequestException ex) {
throw new ApplicationException(ex.Message.ToString());
}
}
}
i've added this component into my Blazor page.
Now, when i shut down my API server and run my Blazor app, i even get this error.
My Question is: Is there any way to catch this error?