Example #1
0
        public override void OnActionExecuting(ActionExecutingContext actionContext)
        {
            var request = actionContext.HttpContext.Request;
            if (!request.IsAjaxRequest())
            {
                actionContext.HttpContext.Response.Clear();
                actionContext.HttpContext.Response.StatusCode = (int)HttpStatusCode.NotAcceptable;
                GenericErrorModel ajaxErrorModel = new GenericErrorModel();
                ajaxErrorModel.AddMessage("Invalid request");

                actionContext.Result = new JsonResult
                {
                    Data = ajaxErrorModel,
                    JsonRequestBehavior = JsonRequestBehavior.AllowGet,
                    ContentEncoding = System.Text.Encoding.UTF8,
                    ContentType = "application/json",
                };
            }
        }
Example #2
0
 public ActionResult Send(NewMessageModel chatMessage)
 {
     if (ModelState.IsValid)
     {
         _messageRepo.Insert(new ChatMessage
             {
                 SendTime = DateTimeOffset.UtcNow,
                 Text = chatMessage.Message,
                 UserName = chatMessage.UserName
             });
         var model = new MessageListModel
         {
             Messages = _messageRepo.GetLatestAfterId(chatMessage.LastCheckedId ?? 0).Select(m => new ChatMessageDto(m))
         };
         return Json(model);
     }
     GenericErrorModel errorModel = new GenericErrorModel().SetModelErrors(ViewData);
     return Json(errorModel);
 }