Pages

Tuesday 18 March 2014

Showing Custom errors in mvc4

To show user defined error messages instead of system exception following code are used.

It is necessary that developer must show user defined message that are understandable to common people who are using the system.

So to use custom user message set following code into web.config

<customErrors mode="On" defaultRedirect="~/System/SystemError">
      <error statusCode="404" redirect="~/System/PageNotFound"/>
    </customErrors>

Here I created one controller System which will be called when any exception come while executing the application.

System controller contains following code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ABC.Web.Common;

namespace ABC.Web.Controllers
{
    [CustomAuthorizeAttribute]
    public class SystemController : Controller
    {
        public ActionResult SessionExpired()
        {
            return View();
        }
        public ActionResult PageNotFound()
        {
            return View();
        }
        public ActionResult SystemError()
        {
            return View();
        }
    }
}

Now view is created for each action and that show proper user message.
Following view For Page Not found Message

Page Not Found View:

@{
    ViewBag.Title = "PageNotFound";
    Layout = "~/Views/Shared/_LayoutLogin.cshtml";
   }
<br/>
<br/>
<center>
<div style="width: 335px; height: 320px; ">
<h1 style="Font-size: xx-large ">Page Not Found</h1>
</div>
</center>

No comments:

Post a Comment