예제 #1
0
        public async Task <ActionResult> Signup([FromForm] Account account)
        {
            AccountContext accountContext = HttpContext.RequestServices.GetService(typeof(dotNet期末项目.Models.AccountContext)) as AccountContext;

            //如果该用户名已被注册
            if (await accountContext.SelectOne(account.Username) != null)
            {
                return(BadRequest("用户名已存在"));
            }

            /*
             * 【说明】此处用 await 执行 addAccount 函数时,不会同步执行后面的 if(result) 语句,而是说执行该请求的线程能同时执行其他的而请求。
             */
            bool result = await accountContext.AddAcount(account);

            if (result)
            {
                //创建默认头像
                string source = Path.Combine(_hostingEnvironment.WebRootPath, "img", "avator.jpg");
                string dest   = Path.Combine(_hostingEnvironment.WebRootPath, "avator", account.Username + ".jpg");
                System.IO.File.Copy(source, dest, true);

                return(Ok("成功注册账号"));
            }

            return(StatusCode(500, "数据库错误"));
        }