user20358
user20358

Reputation: 14736

Does HandleError work anymore in Asp.net MVC 4 with razor

I have the [HandleError] attribute set on my controller.

This is my action method:

    public ActionResult ShowError()
    {

        throw new NullReferenceException();
        return View();
    }

This action method has a corresponding view.

I have set custom errors in the config

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

I have the Error.cshtml file in the shared folder under Views folder.

Now navigating to the ShowError controller method brings up this window in my IDE enter image description here

On hitting F5 I get this in my browser window instead of the Error page from the shared folder. enter image description here

What else is needed to be done to show the error page? How do I get the HandleError to work?

Thanks

Upvotes: 3

Views: 3131

Answers (3)

ChrisP
ChrisP

Reputation: 10116

Your can put the name of the view in defaultRedirect="Error" as described by frictionlesspully or it can be specified in the HandleError attribute on an action, controller, or globally.

[HandleError(View="Error")]

The view can be named something other than "Error" if you specify it explicitly, e.g., "ServerError"

[HandleError(View="ServerError")]

You can also create a sub-folder w/in /Shared to group multiple error views if you want flexibility

/Shared/Errors/Server.cshtml

and then use the path in the attribute

[HandleError(View="Errors/Server")]

The folder where the controller and the /Shared folder are searched for the error view.

Upvotes: 0

user20358
user20358

Reputation: 14736

Ok. So the problem was the browser I was using, which is IE 9. IE 9 seems to have problems showing an error page that is less than one KB. I put @(new String(' ', 1000)) at the end of my error page and got it working.

Upvotes: 2

frictionlesspulley
frictionlesspulley

Reputation: 12368

the documentation states that

To enable custom error handling for use by a HandleErrorAttribute filter, add a customErrors element to the system.web section of the application's Web.config file, as shown in the following example:

<system.web>
  <customErrors mode="On" defaultRedirect="Error" />
</system.web>

Upvotes: 1

Related Questions