public async Task AddAsync(BloggAddModel model) { MultipartFormDataContent formData = new MultipartFormDataContent(); if (model.Image.FileName != null) { // var bytes = await System.IO.File.ReadAllBytesAsync(model.Image.FileName);// performansli fakat sikintili bende hata verdi var stream = new MemoryStream(); await model.Image.CopyToAsync(stream); var bytes = stream.ToArray(); ByteArrayContent byteContent = new ByteArrayContent(bytes); byteContent.Headers.ContentType = new MediaTypeHeaderValue(model.Image.ContentType); formData.Add(byteContent, nameof(BloggAddModel.Image.Name), model.Image.FileName); } var user = _httpContextAccessor.HttpContext.Session.GetObject <AppUserViewModel>("activeUser"); model.AppUserId = user.Id; //buradaki olay web te form dan nasil veri database e gonderilir onu goruyoruz bakiniz stringcontent clasii ile string olarak tum datalari gonderiyoruz (image haric onu yukarida byte olarak gonderdik) daha sonra ise gidecegi prop adini veriyoruz yani bu prop adi bizim burdaki model ve database deki model le eslesen ad nameof(BloggAddMode.AppUserId vs vs) formData.Add(new StringContent(model.AppUserId.ToString()), nameof(BloggAddModel.AppUserId)); formData.Add(new StringContent(model.ShortDescription), nameof(BloggAddModel.ShortDescription)); formData.Add(new StringContent(model.Description), nameof(BloggAddModel.Description)); formData.Add(new StringContent(model.Title), nameof(BloggAddModel.Title)); ///tabi biz bunlari gonderdik deee appuser iin bilgiiisni de gondermemiz lazim yukari da ilk islem _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _httpContextAccessor.HttpContext.Session.GetString("token")); await _httpClient.PostAsync("", formData); }
public async Task <IActionResult> Create(BloggAddModel model) { if (ModelState.IsValid) { await _bloggApiService.AddAsync(model); return(RedirectToAction("Index")); } return(View(model));//valid degilse modeli geri don sayfayi yani }