In this section I will explain about making of custom error page. We have seen in most of the cases default error page occur. Now we will make our custom error. We can design it according to our need.

Below are the few steps for creating our error page.

First of all add shared folder in the view folder 

Making Custom Error Page in Mvc4

In the shared folder add a view for error view. We can design as want. I had make a simple error page.

 

@{
    Layout = null;
}
 
<!DOCTYPE html>
 
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>error</title>
</head>
<body>
    <div>
        <h2>Sorry !!! this view is not available</h2>
    </div>
</body>
</html>


  Now add a Home controller and add following code.

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
 
namespace MvcApplication6.Controllers
{
    
    public class HomeController : Controller
    {
        //
        // GET: /Home/
       [HandleError]
        public ActionResult Index()
        {
          
            return View();
        }
        public ActionResult Add()
        {
            return View();
        }
 
    }
}
 
It is set for particular method we can set it all the method for this do like this.
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
 
namespace MvcApplication6.Controllers
{
     [HandleError]
    public class HomeController : Controller
    {
        //
        // GET: /Home/
      
        public ActionResult Index()
        {
          
            return View();
        }
        public ActionResult Add()
        {
            return View();
        }
 
    }
}

 

In the home controller add two method Index() and Add() . HandleError attribute render error page in share folder when error occur and one more important thing is to set customError mode on of web. config root. In System.Web section. There are two web.config in the application in the view folder and in the root .we have to change in the root web.config.

  Modified On Mar-17-2018 04:25:43 AM

Leave Comment