Beispiel #1
0
        public async Task <ActionResult> Create(UserViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }
            try
            {
                if (model.AvatarPhoto != null)
                {
                    string folder     = "images/cover/user/";
                    var    extensions = model.AvatarPhoto.FileName.Split('.');
                    var    extension  = extensions[extensions.Length - 1];
                    folder         += model.Id.ToString() + "." + extension;
                    model.AvatarURL = "/" + folder;
                    string serverFolder    = Path.Combine(_webHostEnvironment.WebRootPath, folder);
                    var    copyImageStream = new FileStream(serverFolder, FileMode.Create);
                    model.AvatarPhoto.CopyTo(copyImageStream);
                    copyImageStream.Close();
                }
                model.AvatarPhoto = null;
                var token            = CookieEncoder.DecodeToken(Request.Cookies["access_token_cookie"]);
                var createUserResult = await _userManage.CreateUser(model, token);

                //error
                if (createUserResult.msg != null || createUserResult.data == null)
                {
                    ViewData["Error"] = createUserResult.msg;
                    return(View(model));
                }
                //success
                return(RedirectToAction(nameof(Index)));
            }
            catch
            {
                ViewData["Error"] = "Can't connect to server. Please try later.";
                return(View());
            }
        }