コード例 #1
0
        public async Task <ActionResult> Create(InputFriendModel amigo)
        {
            var access_token = Session["access_token"];

            var data = new Dictionary <string, string>
            {
                { "Nome", amigo.Nome },
                { "Sobrenome", amigo.Sobrenome },
                { "Email", amigo.Email },
                { "Telefone", amigo.Telefone },
                { "Aniversario", amigo.Aniversario }
            };

            using (var cliente = new HttpClient()) {
                cliente.BaseAddress = new Uri(UrlDefault);
                cliente.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", $"{access_token}");

                using (var requestContent = new FormUrlEncodedContent(data)) {
                    var response = await cliente.PostAsync("/api/friend/create", requestContent);

                    if (response.IsSuccessStatusCode)
                    {
                        return(RedirectToAction("Index"));
                    }
                }
                return(View());
            }
        }
コード例 #2
0
        public async Task <ActionResult> Delete(InputFriendModel amigo, int id)
        {
            var access_token = Session["access_token"];

            using (var client = new HttpClient()) {
                client.BaseAddress = new Uri(UrlDefault);
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", $"{access_token}");

                var response = await client.DeleteAsync($"api/friend/{id}");

                if (response.IsSuccessStatusCode)
                {
                    return(RedirectToAction("Index"));
                }
            }

            return(View());
        }
コード例 #3
0
        public async Task <ActionResult> Details(int id)
        {
            var access_token = Session["access_token"];

            var friend = new InputFriendModel();

            using (var client = new HttpClient()) {
                client.BaseAddress = new Uri(UrlDefault);
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", $"{access_token}");

                var response = await client.GetAsync($"api/friend/{id}");

                if (response.IsSuccessStatusCode)
                {
                    var responseContent = await response.Content.ReadAsStringAsync();

                    friend = JsonConvert.DeserializeObject <InputFriendModel>(responseContent);
                    return(View(friend));
                }
            }
            return(View());
        }