コード例 #1
0
        public ActionResult Create(Post post)
        {
            try
            {
                var       userName    = User.Identity.Name;
                User      currentUser = _db.Users.FirstOrDefault(user => string.Equals(user.UserName, userName));
                WebClient webCLient   = new WebClient();
                string    source      = webCLient.DownloadString(post.URL);
                string    title       = Regex.Match(source, @"\<title\b[^>]*\>\s*(?<Title>[\s\S]*?)\</title\>", RegexOptions.IgnoreCase).Groups["Title"].Value;


                post.RelatedUserId = currentUser.UserId;
                post.User          = currentUser;
                post.Title         = title;

                _db.Posts.Add(post);
                _db.SaveChanges();
            }
            catch (DbEntityValidationException ex)
            {
                throw;
                //Log
            }
            catch (DbUpdateException ex)
            {
                throw;
                //Log
            }
            catch (Exception ex)
            {
                throw;
                //Log
            }
            return(RedirectToAction("Index"));
        }
コード例 #2
0
 private void RegisterUser(User user)
 {
     _db.Users.Add(user);
     _db.SaveChanges();
 }