Custom Error Page does not show even though I set CustomError="On"

12,368

Solution 1

Here is step by step guide to doing this,

To start with, first set the “customErrors’s” mode property to ‘on’. (This specifies that custom errors are enabled.)

1. Code from Web.config

<system.web>

    <customErrors mode="On"></customErrors>

</system.web>

Below is my controller code, I have specified the HandleError attribute on the controller class. Also to generate an error I am throwing an exception from the Index action.

2. Code from HomeController.cs

[HandleError]
public class HomeController : Controller
{
    public ActionResult Index()
    {
    throw new Exception("Error Occured !");
    ViewBag.Message = "Welcome to ASP.NET MVC!";

    return View();
    }

    public ActionResult About()
    {
    return View();
    }
}

As you can see in the screenshot below, I have a Error.cshtml view defined inside the Shared folder of the Views folder.

3. Solution Explorer

enter image description here

Note that the Error.cshtml view (shown below) uses a model of ‘System.Web.Mvc.HandleErrorInfo’

4. Code from Error.cshtml

@model System.Web.Mvc.HandleErrorInfo

@{
    ViewBag.Title = "Error";
}

<h2>
    Sorry, an error occurred while processing your request.
</h2>

On running the project, instead of getting the yellow death screen now I get the following output.

enter image description here

Taken from this article

Update :

Try this out by creating a new project, this should and does work. I suspect that your Error.cshtml file may not have model as @model System.Web.Mvc.HandleErrorInfo or you may be having multiple Error.cshtml pages in different view folders.

Solution 2

I fixed mine.. It's actually the *_Layout* page cannot be rendered because my _Layout.cshtml was strongly typed to a base view model which HandleErrorInfo can't be casted into. And you can never see this page binding error because it only happens when customError is on... that's just great.

Solution 3

One reason that could lead to what you are experiencing:

Something in your _layout is causing an exception to throw when the 'Error.cshtml' is rendering.

Do a test adding

@{
    Layout = null;
}

in your 'Error.cshtml' file.

Solution 4

I had the same problem when I was updating the wrong web.config file.

There is a web.config in the Views folder and also under the project application site.

The solution would be to update web.config located under project.

Share:
12,368
LFish
Author by

LFish

Updated on July 28, 2022

Comments

  • LFish
    LFish almost 2 years

    I am using Visual Studio 2012 with the MVC 4 Framework and would like to see the friendly error page of the shared folder, if an exception is unhandled. (I know, as a develepor I need to see the error details but I just wanted to test this setting)
    So far I know that adding the following code in the system.web section of web.config should do the trick

    <customErrors mode="On" />
    

    Unfortunately instead of my friendly error page I get this:

    Server Error in '/' Application.

    Runtime Error Description: An application error occurred on the server. The current custom error settings for this application prevent the details of the application error from being viewed.

    Details: To enable the details of this specific error message to be viewable on the local server machine, please create a tag within a "web.config" configuration file located in the root directory of the current web application. This tag should then have its "mode" attribute set to "RemoteOnly". To enable the details to be viewable on remote machines, please set "mode" to "Off".

     <!-- Web.Config Configuration File -->
    
     <configuration>
         <system.web>
             <customErrors mode="RemoteOnly"/>
         </system.web> </configuration> 
    

    Notes: The current error page you are seeing can be replaced by a custom error page by modifying the "defaultRedirect" attribute of the application's configuration tag to point to a custom error page URL.

     <!-- Web.Config Configuration File -->
    
     <configuration>
         <system.web>
             <customErrors mode="On" defaultRedirect="mycustompage.htm"/>
         </system.web> </configuration>
    


    I would like to do it as described and not with a workaround like adding an action to HomeController, setting routes or something.
    A reference that this should work is the following:
    http://pluralsight.com/training/players/PSODPlayer?author=scott-allen&name=mvc3-building-controllers&mode=live&clip=0&course=aspdotnet-mvc3-intro
    (On left select the chapter "Action Filters" and skip to minute 3:30)


    UPDATE (link changed):
    http://pluralsight.com/training/Player?author=scott-allen&name=mvc4-building-m2-controllers&mode=live&clip=0&course=mvc4-building
    (On top click on the thin line that states "Controller in ASP.NET MVC 4", then click on "Action Filters" and skip to minute 5:00)

    Does anybody have a clue why I cannot see the friendly error page with the setting mentioned above?
    Thanks in advance!

  • LFish
    LFish over 11 years
    Unfortunately, for me it still did not work. I've done as you told me. Furthermore, the HandleErrorAttribute is registered by default in global.asax so I usually don't need to set that Attribute for the Homecontroller.
  • LFish
    LFish over 10 years
    Sorry for my late answer ;) but I already had updated the right web.config as you mentioned. Anyway the problem still persists.
  • Rajeev
    Rajeev over 10 years
    I am on MVC3 and just added the below settings to web.config and custom page and it works for me. <customErrors mode="On" defaultRedirect="~/Views/Shared/Error.cshtml"> <error redirect="~/Home/NotFound" statusCode="404" /> '</customErrors>
  • Ande
    Ande over 9 years
    @L4_ If it is still not working, see the answer about editing the right web.config - that's what was causing the issue in my case.
  • Omar.Ebrahim
    Omar.Ebrahim over 6 years
    I'm glad I found this. I was going mad because 1) I was editing the wrong web.config, and 2) I didn't have the model as System.Web.Mvc.HandleErrorInfo. +1!
  • IfElseTryCatch
    IfElseTryCatch about 6 years
    I am "do'hing!" so hard right now! Thanks for the tip!