Beispiel #1
1
        public override void OnException(ExceptionContext filterContext)
        {
            if (filterContext == null)
            {
                throw new ArgumentNullException("filterContext");
            }

            // If custom errors are disabled, we need to let the normal ASP.NET exception handler
            // execute so that the user can see useful debugging information.
            if (filterContext.ExceptionHandled || !filterContext.HttpContext.IsCustomErrorEnabled)
            {
                return;
            }

            Exception exception = filterContext.Exception;

            // If this is not an HTTP 500 (for example, if somebody throws an HTTP 404 from an action method),
            // ignore it.
            if (new HttpException(null, exception).GetHttpCode() != 500)
            {
                return;
            }

            if (!ExceptionType.IsInstanceOfType(exception))
            {
                return;
            }

            string controllerName = (string)filterContext.RouteData.Values["controller"];
            string actionName = (string)filterContext.RouteData.Values["action"];
            HandleErrorInfo model = new HandleErrorInfo(filterContext.Exception, controllerName, actionName);

            if (this._logger.IsErrorEnabled)
            {
                this._logger.Error(string.Format("An unexpected error occured while executing {0} in {1}.", actionName, controllerName), exception);
            }

            MessageViewData messageViewData = new MessageViewData();

            while (exception != null)
            {
                messageViewData.AddErrorMessage(this._localizer.GetString(exception.Message));
                exception = exception.InnerException;
            }
            var viewData = new ViewDataDictionary<HandleErrorInfo>(model);
            viewData["Messages"] = messageViewData;

            // Render error view
            filterContext.Result = new ViewResult
                                   	{
                                   		ViewName = View,
                                   		MasterName = Master,
                                   		ViewData = viewData,
                                   		TempData = filterContext.Controller.TempData
                                   	};

            filterContext.ExceptionHandled = true;
            filterContext.HttpContext.Response.Clear();
            filterContext.HttpContext.Response.StatusCode = 500;
        }
Beispiel #2
0
        /// <summary>
        /// Gets a <see cref="MessageViewData"></see> object for the messages that were added
        /// during the current request.
        /// </summary>
        /// <returns></returns>
        public MessageViewData GetFlashMessages()
        {
            MessageViewData flashMessageViewData = new MessageViewData();

            foreach (KeyValuePair <string, IList <string> > flashMessagesForKey in this._flashMessages)
            {
                flashMessageViewData.Messages[flashMessagesForKey.Key] = flashMessagesForKey.Value;
            }
            return(flashMessageViewData);
        }
Beispiel #3
0
 private void InitMessageViewData(ActionExecutingContext filterContext)
 {
     var currentController = filterContext.Controller;
     this._tempData = currentController.TempData;
     if (this._tempData.ContainsKey("Messages"))
     {
         this._messageViewData = new MessageViewData((MessageViewData)this._tempData["Messages"]);
     }
     else
     {
         this._messageViewData = new MessageViewData();
     }
     this._messageViewData.FlashMessageAdded += new EventHandler(MessageViewData_FlashMessageAdded);
     currentController.ViewData["Messages"] = this._messageViewData;
 }
Beispiel #4
0
        /// <summary>
        /// Creates a new instance of the <see cref="MessageViewData"></see> class.
        /// </summary>
        /// <param name="messagesFromPreviousRequest">Messages from the previous request (TempData)</param>
        public MessageViewData(MessageViewData messagesFromPreviousRequest)
        {
            Initialize();

            if (messagesFromPreviousRequest != null)
            {
                foreach (KeyValuePair <string, IList <string> > messages in messagesFromPreviousRequest.Messages)
                {
                    this._messages[messages.Key] = messages.Value;
                }
                foreach (KeyValuePair <string, object[]> messageParam in messagesFromPreviousRequest.MessageParams)
                {
                    this._messageParams.Add(messageParam.Key, messageParam.Value);
                }
            }
        }
Beispiel #5
0
        /// <summary>
        /// Creates a new instance of the <see cref="MessageViewData"></see> class.
        /// </summary>
        /// <param name="messagesFromPreviousRequest">Messages from the previous request (TempData)</param>
        public MessageViewData(MessageViewData messagesFromPreviousRequest)
        {
            Initialize();

            if (messagesFromPreviousRequest != null)
            {
                foreach (KeyValuePair<string, IList<string>> messages in messagesFromPreviousRequest.Messages)
                {
                    this._messages[messages.Key] = messages.Value;
                }
                foreach (KeyValuePair<string, object[]> messageParam in messagesFromPreviousRequest.MessageParams)
                {
                    this._messageParams.Add(messageParam.Key, messageParam.Value);
                }
            }
        }
Beispiel #6
0
 /// <summary>
 /// Gets a <see cref="MessageViewData"></see> object for the messages that were added 
 /// during the current request.
 /// </summary>
 /// <returns></returns>
 public MessageViewData GetFlashMessages()
 {
     MessageViewData flashMessageViewData = new MessageViewData();
     foreach (KeyValuePair<string, IList<string>> flashMessagesForKey in this._flashMessages)
     {
         flashMessageViewData.Messages[flashMessagesForKey.Key] = flashMessagesForKey.Value;
     }
     return flashMessageViewData;
 }