public async Task <ActionResult> Create(InputBookView model) { var data = new Dictionary <string, string> { { "Title", model.Title }, { "ISBN", model.ISBN }, { "Year", model.Year.ToString() }, { "IdAuthor", model.IdAuthor.ToString() } }; var access_token = Session["access_token"]; using (var client = new HttpClient()) { client.BaseAddress = new Uri("http://localhost:60453"); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", $"{access_token}"); using (var requestContent = new FormUrlEncodedContent(data)) { var response = await client.PostAsync("api/Book", requestContent); if (response.IsSuccessStatusCode) { return(RedirectToAction("Index")); } return(View("Error")); } } }
public async Task <ActionResult> Edit(int Id) { var book = new InputBookView(); var access_token = Session["access_token"]; using (var client = new HttpClient()) { client.BaseAddress = new Uri("http://localhost:60453"); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", $"{access_token}"); var books_resopnse = await client.GetAsync($"api/Book/{Id}"); var responseContent = await books_resopnse.Content.ReadAsStringAsync(); book = JsonConvert.DeserializeObject <InputBookView>(responseContent); } var authors = new List <AuthorsViewModel>(); using (var client = new HttpClient()) { client.BaseAddress = new Uri("http://localhost:60453"); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", $"{access_token}"); var authors_response = await client.GetAsync($"api/author"); var responseContent = await authors_response.Content.ReadAsStringAsync(); authors = JsonConvert.DeserializeObject <List <AuthorsViewModel> >(responseContent); } ViewBag.IdAuthor = new SelectList( authors, "Id", "Name" ); Session.Add("id_book", Id); return(View(book)); }