public async void Create(IFormFile file, string caption) { string fileName = file.FileName; int id = HttpContext.Session.GetInt32("id") ?? default(int); _database.posts.Add(new Post { image = fileName, caption = caption, user_id = id, datetime = DateTime.Now }); _database.SaveChanges(); var uploads = Path.Combine(_environment.WebRootPath, "images"); var filePath = Path.Combine(uploads, fileName); using (var fileStream = new FileStream(filePath, FileMode.Create)) { await file.CopyToAsync(fileStream); } Response.Redirect("Index"); }
public void Create(string username, string password) { var user = _database.users.SingleOrDefault(c => c.username == username); if (user != null) { TempData["FlashMessage"] = "Username already in use"; Response.Redirect("New"); } else { _database.users.Add(new User { username = username, password = password }); _database.SaveChanges(); HttpContext.Session.SetString("username", username); var newuser = _database.users.SingleOrDefault(c => c.username == username); HttpContext.Session.SetInt32("id", newuser.id); Response.Redirect("../Home"); } }