0

I am new to web development and I need to display custom error message that comes from IIS Server in MVC-5 application. for example "504 gateway time out error" or "500 Error". I am working on already existing code. Below is what I have till now.

BaseController:

protected override void OnException(ExceptionContext filterContext)
        {
            if (!filterContext.ExceptionHandled)
            {
                filterContext.ExceptionHandled = true;
                Logger.Error("SimController", "OnException", "An exception has occurred.", filterContext.Exception);

                // this will cause the error to be displayed to the user..
                Response.StatusCode = 500;

                Response.StatusDescription = Constants.DISPLAYED_ERROR_MESSAGE_SHARED;

                if (filterContext.Exception.GetType() == typeof(ValidationException))
                { Response.StatusDescription += Constants.ADDITIONAL_DETAIL_ERROR_MESSAGE + filterContext.Exception.Message; }
                else
                { Response.StatusDescription += Constants.GENERIC_ERROR_MESSAGE; }
            }
        }

ErrorController:

 public ActionResult Index(string Message)
        {
            // We choose to use the ViewBag to communicate the error message to the view
            ViewBag.Message = Message;
            return View();
        }
        public ActionResult Oops(string Message)
        {
            // We choose to use the ViewBag to communicate the error message to the view
            ViewBag.Message = Message;
            return View();
        }

Web.Config

 <system.web>
    <compilation debug="true" targetFramework="4.5.1" />
    <httpRuntime targetFramework="4.5" />
    <customErrors mode="On" defaultRedirect="~/ErrorPage/Oops">
      <error redirect="~/Error/Oops/404" statusCode="404" />
      <error redirect="~/Error/Oops/500" statusCode="500" />
    </customErrors>
  </system.web>

Global.asax

 public class FilterConfig
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            //filters.Add(new HandleErrorAttribute());
        }
    }



protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);

    }

I am new to coding so I have put whatever I could in this post. So, is that it? I am still not able to see the custom page for 500.

The way I am simulating the error is

HomeController:

 public ActionResult Index()
 {
      return new HttpStatusCodeResult(500); 
      return View();
 }

Post I referrred : Custom Error

Community
  • 1
  • 1
Unbreakable
  • 7,776
  • 24
  • 90
  • 171

1 Answers1

0
  1. You did not create views for ErrorController.
  2. Oops(string Message) takes message as an argument. You pass there error code: redirect="~/Error/Oops/404".
  3. Do not change 'Global.asax' file. It is enough to <customErrors mode="On">.
  • Thanks for the response. I will try it on Monday and will get back to you. As per the suggestion give by you I will create a simple view. As far as `point 2 and 3` are concerned is there any action required on my part or it was just for information purpose? Or I need to make some changes to abode by point 2 and 3? – Unbreakable Jan 15 '17 at 22:21
  • Also, the way I am simulating the error that's fine right? – Unbreakable Jan 15 '17 at 22:21
  • Also, if you see the topmost code snippet that I have pasted. `on exception` method breakpoint gets hit if something goes wrong in the code. Is that normal behaviour. I hope that will not affect my Custom error handling. Kindly guide me. – Unbreakable Jan 15 '17 at 22:23
  • What is the result of `/Home/Index`at the moment? – Sebastian Xawery Wiśniowiecki Jan 15 '17 at 22:41