Language: C#
ErrorController with generic Message Display from anywhere in ASP.NET
public class ErrorController : baseController { /// <summary> /// Displays a generic error message /// </summary> /// <param name="title"></param> /// <param name="message"></param> /// <param name="redirectTo"></param> /// <returns></returns> public ActionResult ShowError(string title, string message, string redirectTo, bool messageIsHtml) { if (string.IsNullOrEmpty(title)) title = "An error occurred."; if (string.IsNullOrEmpty(message)) message = "We are sorry, but an unspecified error occurred in the application. The error has been logged and forwarded to be checked out as soon as possible."; ErrorViewModel model = new ErrorViewModel { Message = message, Title = title, RedirectTo = redirectTo, MessageIsHtml = messageIsHtml }; // Explicitly point at Error View regardless of action return View("Error", model); } public ActionResult ShowMessage(string title, string message, string redirectTo, bool messageIsHtml) { return this.ShowError(title, message, redirectTo, messageIsHtml); } /// <summary> /// Static method that can be called from outside of MVC requests /// (like in Application_Error) to display an error View. /// </summary> /// <param name="title"></param> /// <param name="message"></param> /// <param name="redirectTo"></param> /// <param name="messageIsHtml"></param> public static void ShowErrorPage(string title, string message, string redirectTo) { ErrorController controller = new ErrorController(); RouteData routeData = new RouteData(); routeData.Values.Add("controller", "Error"); routeData.Values.Add("action", "ShowError"); routeData.Values.Add("title", title); routeData.Values.Add("message", message); routeData.Values.Add("messageIsHtml", true); ((IController)controller).Execute(new RequestContext(new HttpContextWrapper(System.Web.HttpContext.Current), routeData)); } /// <summary> /// Static method that can be called from outside of MVC requests /// (like in Application_Error) to display an error View. /// </summary> public static void ShowErrorPage(string title, string message) { ShowErrorPage(title, message, null); } } public class ErrorViewModel { public string Title = string.Empty; public string Message = string.Empty; public string RedirectTo = string.Empty; public bool MessageIsHtml = false; } // call like this (for example in Application_Error ErrorController.ShowErrorPage("Access Denied", "You've accessed a resource that requires a valid login. " + StockMessage);
Tags:
Description:
This Error Controller allows for generic error display
Report Abuse
Subscribe
Discuss
What's new
What is it
New Snippet
Recent Snippets
My Snippets
Web Code
Search

