AspBucket offers ASP.NET, C#, VB, Jquery, CSS, Ajax, SQL tutorials. It is the best place for programmers to learn

Wednesday, 30 September 2015

How to make custom error page in ASP.NET MVC ?

In this article we are going to discuss how to create custom error page in ASP.NET MVC ? Initially you need to create a error controller.




 public class ErrorController : Controller
 {
 
    // GET: /Error/ 
    public ActionResult Index()
    { 
       return View();
    } 
 
    // GET: 404 NotFound Error 
    public ViewResult NotFound() 
    { 
       return View("NotFound"); 
    } 
 }

Now create the view under "Views/Error/NotFound.cshtml" as.

 @{   Layout = "~/Views/Shared/_Layout.cshtml";   }
 
      404, Page not found.



create another view under "Views/Error/Index.cshtml" as.

 @{  Layout = "~/Views/Shared/_Layout.cshtml";   }
 
     Error Occurred.



Then Make changes in webconfig file under system.web tag
 <system.web>  
   <customErrors mode="On" defaultRedirect="~/Error/Index">   
   <error redirect="~/Error/NotFound" statusCode="404" />   
   </customErrors>  
 </system.web>  

0 comments :

Post a comment

  • Popular Posts
  • Comments