Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Api Xamarin,如果状态代码不正常,则重定向到页面_Api_Xamarin_Xamarin.forms - Fatal编程技术网

Api Xamarin,如果状态代码不正常,则重定向到页面

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

我有RESTAPI,它通过电子邮件返回用户是否存在

若用户存在,并且我正在从API恢复状态OK,则一切正常,但当API响应404时,我的应用程序崩溃。我不知道如何在应用程序崩溃之前检查状态是否正常,并在API找不到用户的情况下将用户重定向到注册页面

下面是向api发出请求的代码:

        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;
            }
        }