Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC 3 Custom/Http error pages not shown for View exceptions

I have custom/http errors set up in an ASP.NET MVC 3 application that will be shown on errors. My Web.Config's httpErrors section looks like this:

    <httpErrors errorMode="Custom" existingResponse="Replace">
        <remove statusCode="400" />
        <remove statusCode="403" />
        <remove statusCode="404" />
        <remove statusCode="408" />
        <remove statusCode="500" />
        <remove statusCode="503" />
        <error statusCode="400" responseMode="ExecuteURL" path="/Error/BadRequest" />
        <error statusCode="403" responseMode="ExecuteURL" path="/Error/Forbidden" />
        <error statusCode="404" responseMode="ExecuteURL" path="/Error/NotFound" />
        <error statusCode="408" responseMode="ExecuteURL" path="/Error/Timeout" />
        <error statusCode="500" responseMode="ExecuteURL" path="/Error/InternalServerError" />
        <error statusCode="503" responseMode="ExecuteURL" path="/Error/ServiceUnavailable" />
    </httpErrors>

I haven't set the customerErrors setting as this doesn't work well with MVC & IIS 7.5.

All works fine, when an exception occurs in code.

However, when an exception is thrown in a view - it tries to show the default ~/Views/Shared/Error.cshtml which I don't have.

The view that might throw an error could be this:

@Html.Partial("TemplateSection", Model.PreContent)

And imagine PreContent is null, and the TemplateSection view will fail when displaying values by doing this: @Model.Name

like image 840
MartinHN Avatar asked May 31 '12 21:05

MartinHN


1 Answers

I read this article and have had no problems since dropping my resulting code into production 48 hours ago. Give it a whirl.

like image 134
bluevector Avatar answered Nov 15 '22 09:11

bluevector