custom error pages and error handling in asp.net mvc 4

Jak
Jak
Member
364 Points
152 Posts

Hi,

How can I make custom error pages and error handling in asp.net mvc 4? I have enabled Custom Error in Web.config but not working.

<customErrors mode="On" defaultRedirect="Error.htm">
     <error statusCode="400" redirect="Error.htm"/>
     <error statusCode="404" redirect="PageNotFound.htm"/>
</customErrors>
Views: 8167
Total Answered: 2
Total Marked As Answer: 1
Posted On: 24-Dec-2015 10:13

Share:   fb twitter linkedin
Answers
NiceOne Team
NiceOne...
Admin
404 Points
67 Posts
         

Hi Jak,

You can use view page with controller. Then the Modified web.config as:

<customErrors mode="On" defaultRedirect="Error/UnkownError">
<error statusCode="400" redirect="Error/UnknownError"/>
<error statusCode="404" redirect="Error/PageNotFound"/>
</customErrors>

 And Create new Controller 'Error' as:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebSolution.Class;
namespace WebSolution.Controllers
public class ErrorController : Controller
public ActionResult Error()
return View();
}
 
public ActionResult NotFound()
return View();
}
}
}

 

And In RouteConfig.cs file add following:

routes.MapRoute( 
"ErrorCustom", // Route name 
"Error/UnknownError", // URL with parameters 
new { controller = "Error", action = "Error" } // Parameter defaults
);
routes.MapRoute( 
"ErrorNotFound", // Route name 
"Error/PageNotFound", // URL with parameters 
new { controller = "Error", action = "NotFound" } // Parameter defaults
);

 Now try the above approch.

  

Posted On: 28-Dec-2015 00:36
Rahul Kiwitech
Rahul K...
Participant
100 Points
40 Posts
         

It's wonderful! great

Posted On: 29-Jun-2016 06:09
 Log In to Chat