Api Xamarin,如果状态代码不正常,则重定向到页面
我有RESTAPI,它通过电子邮件返回用户是否存在 若用户存在,并且我正在从API恢复状态OK,则一切正常,但当API响应404时,我的应用程序崩溃。我不知道如何在应用程序崩溃之前检查状态是否正常,并在API找不到用户的情况下将用户重定向到注册页面 下面是向api发出请求的代码:Api Xamarin,如果状态代码不正常,则重定向到页面,api,xamarin,xamarin.forms,Api,Xamarin,Xamarin.forms,我有RESTAPI,它通过电子邮件返回用户是否存在 若用户存在,并且我正在从API恢复状态OK,则一切正常,但当API响应404时,我的应用程序崩溃。我不知道如何在应用程序崩溃之前检查状态是否正常,并在API找不到用户的情况下将用户重定向到注册页面 下面是向api发出请求的代码: string getuserUrl = $"https://localhost:99282/api/users/{email}"; var client = new HttpClien
string getuserUrl = $"https://localhost:99282/api/users/{email}";
var client = new HttpClient();
var uri = new Uri(getuserUrl);
var result = await client.GetStringAsync(uri);
var userResult = JsonConvert.DeserializeObject<User>(result);
return userResult;
string getuserUrl=$”https://localhost:99282/api/users/{email}”;
var client=新的HttpClient();
var uri=新uri(getuserUrl);
var result=await client.GetStringAsync(uri);
var userResult=JsonConvert.DeserializeObject(结果);
返回用户结果;
您可以使用下面的代码来确定API调用是否成功
String URL = "https://localhost:99282/api/users/{email}";
using (HttpClient client = new HttpClient())
{
client.BaseAddress = new Uri(URL);
try
{
HttpResponseMessage response = await client.GetAsync(URL);
if (response.IsSuccessStatusCode)
{
// Code 200 - If the API call is sucess
// You redirect to another page
}
else
{
// Show alert for failed calls
}
}
catch (Exception ex)
{
return null;
}
}
您可以使用下面的代码来确定API调用是否成功
String URL = "https://localhost:99282/api/users/{email}";
using (HttpClient client = new HttpClient())
{
client.BaseAddress = new Uri(URL);
try
{
HttpResponseMessage response = await client.GetAsync(URL);
if (response.IsSuccessStatusCode)
{
// Code 200 - If the API call is sucess
// You redirect to another page
}
else
{
// Show alert for failed calls
}
}
catch (Exception ex)
{
return null;
}
}