Exemplo n.º 1
0
        public IActionResult Chat()
        {
            ChatMessagesViewModel messages = new ChatMessagesViewModel();

            messages.OldMessages = GetMessages();
            messages.NewMessage  = new Message();

            try
            {
                messages.NewMessage.Author = HttpContext.Session.GetString("authorName");
            }
            catch (Exception e) { }

            return(View("~/Views/ChatView.cshtml", messages));
        }
Exemplo n.º 2
0
        [ValidateAntiForgeryToken] // Prevents cross-site request forgery
        public IActionResult SendNewMessage(ChatMessagesViewModel model)
        {
            try
            {
                HttpContext.Session.SetString("authorName", model.NewMessage.Author);
            }
            catch (Exception e) { }

            // Validate input
            if (ModelState.IsValid)
            {
                using (var context = GetContext())
                {
                    Message message = model.NewMessage;
                    message.Timestamp = DateTime.Now;
                    context.Messages.Add(message);
                    context.SaveChanges();
                }
            }

            return(RedirectToAction("Chat"));
        }