public ActionResult Create(PostViewModel vm) { try { // TODO: Add insert logic here var userId = this.User.FindFirstValue(ClaimTypes.NameIdentifier); //Save image to Root/image string wwwRoot = _hosting.WebRootPath; string fileName = Path.GetFileNameWithoutExtension(vm.ImageFile.FileName); string extension = Path.GetExtension(vm.ImageFile.FileName); vm.AspNetUsersId = userId; vm.Photo = fileName + extension; string path = Path.Combine(wwwRoot + "\\images\\", fileName + extension); using (var fileStream = new FileStream(path, FileMode.Create)) { vm.ImageFile.CopyTo(fileStream); } //File.Move(sourceFile, destFile); _postsService.AddPosts(vm); return(RedirectToAction(nameof(Index))); } catch { return(View()); } }
public IActionResult Post([FromBody] Posts post) { if (post == null) { return(StatusCode(StatusCodes.Status400BadRequest)); } _postService.AddPosts(post); return(Ok("Record inserted successfully")); }