コード例 #1
0
        public async Task <IActionResult> Create([Bind("ID,Text_info,ImgPath,Link_url,Is_active")] Banner banner)
        {
            if (ModelState.IsValid)
            {
                //获取上传图片的后缀
                string TypeStr = Path.GetExtension(banner.ImgPath.FileName).ToLower();
                if (!ImageType.Contains(TypeStr))
                {
                    ModelState.AddModelError("ImgPath", "文件格式不支持,目前只支持png,gif,jpg,jpeg,bmp");
                    return(View(banner));
                }
                string fileName = Path.Combine(_hostingEnvironment.WebRootPath, "BannerPicture", banner.ID.ToString() + TypeStr);
                if (!Directory.Exists(Path.GetDirectoryName(fileName)))
                {
                    Directory.CreateDirectory(Path.GetDirectoryName(fileName));
                }
                using (var stream = new FileStream(fileName, FileMode.Create))
                {
                    banner.ImgPath.CopyTo(stream);
                }

                banner.Img = Path.Combine("BannerPicture", banner.ID.ToString() + TypeStr);
                //banner.Img = fileName.Remove(0,54).ToLower();
                _context.Add(banner);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(banner));
        }
コード例 #2
0
        public async Task <IActionResult> Create([Bind("ID,Name,URLFiled")] Link link)
        {
            if (ModelState.IsValid)
            {
                _context.Add(link);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(link));
        }
コード例 #3
0
        public async Task <IActionResult> Create([Bind("ID,Name,Index")] Category category)
        {
            if (ModelState.IsValid)
            {
                _context.Add(category);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(category));
        }
コード例 #4
0
        public async Task <IActionResult> Create([Bind("ID,Name")] Recommend recommend)
        {
            if (ModelState.IsValid)
            {
                _context.Add(recommend);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(recommend));
        }
コード例 #5
0
        public async Task <IActionResult> Create([Bind("ID,ArticleID,TagID")] TagRelateArticle tagRelateArticle)
        {
            if (ModelState.IsValid)
            {
                _context.Add(tagRelateArticle);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["ArticleID"] = new SelectList(_context.Article, "ID", "Title", tagRelateArticle.ArticleID);
            ViewData["TagID"]     = new SelectList(_context.Tag, "ID", "Name", tagRelateArticle.TagID);
            return(View(tagRelateArticle));
        }
コード例 #6
0
        public async Task <IActionResult> Create([Bind("ID,UserName,PassWord,UserType,ImagePath")] UserInfo userInfo)
        {
            if (ModelState.IsValid)
            {
                var user = _context.UserInfo.FirstOrDefault(a => a.UserName == userInfo.UserName);

                if (user != null)
                {
                    ModelState.AddModelError("UserName", "用户名已存在,请重新选择");
                    return(View(userInfo));
                }

                userInfo.ID        = Guid.NewGuid();
                userInfo.CreatedAt = DateTime.Now;

                //var ImagePaths = Request.Form.Keys;
                //var ImagePath = ImagePaths.FirstOrDefault();

                //获取图片后缀,判定上传是否为图片类型
                string typeStr = Path.GetExtension(userInfo.ImagePath.FileName).ToLower();

                if (!ImageType.Contains(typeStr))
                {
                    ModelState.AddModelError("ImagePath", "文件格式不支持,目前支持png,gif,jpg,jpeg");
                    return(View(userInfo));
                }

                string fileName = Path.Combine(_hostingEnvironment.WebRootPath, "UserPictures", userInfo.ID.ToString(), DateTime.Now.ToString("yyyy-MMddHHmmsss") + typeStr);

                if (!Directory.Exists(Path.GetDirectoryName(fileName)))
                {
                    Directory.CreateDirectory(Path.GetDirectoryName(fileName));
                }

                using (var stream = new FileStream(fileName, FileMode.Create))
                {
                    userInfo.ImagePath.CopyTo(stream);
                }

                userInfo.UserImage = Path.Combine("UserPictures", userInfo.ID.ToString(), DateTime.Now.ToString("yyyy-MMddHHmmsss") + typeStr);
                //userInfo.UserImage = fileName.Remove(0, 54).ToLower();

                //对密码进行加盐取哈希
                userInfo.PassWord = Convert.ToBase64String(SHA1.Create().ComputeHash(System.Text.Encoding.UTF8.GetBytes(userInfo.PassWord + UserInfo.Salt)));
                _context.Add(userInfo);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(userInfo));
        }
コード例 #7
0
        public async Task <IActionResult> Create(int id, [Bind("CommentID,Content,CreatedAt,UserInfoID,ArticleID")] Comment comment)
        {
            if (ModelState.IsValid)
            {
                comment.CreatedAt = DateTime.Now;
                comment.ArticleID = id;

                _context.Add(comment);
                await _context.SaveChangesAsync();

                return(RedirectToAction("Details", "Articles", new{ id }));
            }
            ViewData["UserInfoID"] = new SelectList(_context.UserInfo, "ID", "PassWord", comment.UserInfoID);
            return(View(comment));
        }
コード例 #8
0
        public async Task <IActionResult> Create([Bind("ID,Title,Excerpt,CategoryID,Img,ImgPath,Content,UserInfoID,Views,RecommendID,CreatedAt,ModifiedTime")] Article article)
        {
            if (ModelState.IsValid)
            {
                article.CreatedAt    = DateTime.Now;
                article.ModifiedTime = DateTime.Now;
                _context.Add(article);

                if (article.ImgPath != null)
                {
                    //获取上传图片的后缀,看是否符合标准
                    string typeStr = Path.GetExtension(article.ImgPath.FileName).ToLower();
                    if (!ImageType.Contains(typeStr))
                    {
                        ModelState.AddModelError("ImgPath", "文件格式不支持,目前支持png,gif,jpg,jpeg");
                        return(View(article));
                    }

                    string fileName = Path.Combine(_hostingEnvironment.WebRootPath, "ArticlePicture", article.UserInfoID.ToString(), article.ID.ToString() + typeStr);
                    string dirname  = Path.Combine(_hostingEnvironment.WebRootPath, "ArticlePicture", article.UserInfoID.ToString(), article.ID.ToString());

                    if (!Directory.Exists(Path.GetDirectoryName(dirname)))
                    {
                        Directory.CreateDirectory(Path.GetDirectoryName(dirname));
                    }

                    using (var stream = new FileStream(fileName, FileMode.Create))
                    {
                        article.ImgPath.CopyTo(stream);
                    }
                    article.Img = Path.Combine("ArticlePicture", article.UserInfoID.ToString(), article.ID.ToString() + typeStr);
                    //article.Img = fileName.Remove(0, 54).ToLower();
                }


                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["CategoryID"]  = new SelectList(_context.Set <Category>(), "ID", "Name", article.CategoryID);
            ViewData["RecommendID"] = new SelectList(_context.Set <Recommend>(), "ID", "Name", article.RecommendID);
            ViewData["UserInfoID"]  = new SelectList(_context.Set <UserInfo>(), "ID", "UserName", article.UserInfoID);
            return(View(article));
        }