I am using the latest version of ASP.NET MVC 5
for my web application and it is running through IIS 7.5
which came with Windows 7
. I am struggling to implement httpErrors
in the web.config and to have my breakpoint reached in my error controller.
I have experimented with displaying HTML files and displaying a view from my error controller as error messages. This is my current setup in my web.config:
<system.webServer>
<httpErrors errorMode="Custom" existingResponse="Replace">
<remove statusCode="404" subStatusCode="-1" />
<remove statusCode="500" subStatusCode="-1" />
<error statusCode="404" path="/Error/Http404" responseMode="ExecuteURL" />
<error statusCode="500" path="Http500.html" responseMode="File" />
</httpErrors>
</system.webServer>
I do have an ErrorController class. There is a directory under Views called Error. And in the Error directory are 2 files:
- Http404.cshtml
- Http500.cshtml
When I typed in www.mywebsite.com/foo
the following happens:
- The Application_Error method is hit
- The breakpoint in the Http404 action method in my error controller is not hit
- The page displayed to me is white, nothing on it
- Google Chrome shows me that an HTTP 403 forbidden error was thrown
When I typed in www.mywebsite.com/foo.html
the following happens:
- The Application_Error method is not hit
- The breakpoint in the Http404 action method in my error controller is not hit
- The page displayed to me is white, nothing on it
- Google Chrome shows me that an HTTP 403 forbidden error was thrown
When I change the the httpErrors configuration to that of below then my static HTML page is displayed with the correct HTTP 404 not found error:
<system.webServer>
<httpErrors errorMode="Custom" existingResponse="Replace">
<remove statusCode="404" subStatusCode="-1" />
<error statusCode="404" path="Http404.html" responseMode="File" />
</httpErrors>
</system.webServer>
I have nothing in my Application_Error method:
protected void Application_Error(object sender, EventArgs e)
{
Exception exception = Server.GetLastError();
}
And this what my error controller looks like:
public class ErrorController : Controller
{
public ActionResult Http404()
{
// Not sure if this is needed?
Response.StatusCode = (int)HttpStatusCode.NotFound;
return View();
}
public ActionResult Http500()
{
// Not sure if this is needed?
Response.StatusCode = (int)HttpStatusCode.InternalServerError;
return View();
}
}
I removed the customErrors
configuration from the web.config.
I have the following questions:
- How do I get this fixed to display my views in my error controller?
- Why are some errors hitting the Application_Error method and others not?
- How do I know if it is an ASP MVC error or an IIS error?
- Some errors are caught in Application_Error and others not, how do I log all of them?