Hi,
First of all, quickly what exactly I want to achieve: translate particular exception into the HTTP 404 so the ASP.NET can handle it further.
I am handling exceptions in the ASP.NET (MVC2) this way:
protected void Application_Error(object sender, EventArgs e) {
var err = Server.GetLastError();
if (err == null)
return;
err = err.GetBaseException();
var noObject = err as ObjectNotFoundException;
if (noObject != null)
HandleObjectNotFound();
var handled = noObject != null;
if (!handled)
Logger.Fatal("Unhandled exception has occured in application.", err);
}
private void HandleObjectNotFound() {
Server.ClearError();
Response.Clear();
// new HttpExcepton(404, "Not Found"); // Throw or not to throw?
Response.StatusCode = 404;
Response.StatusDescription = "Not Found";
Response.StatusDescription = "Not Found";
Response.Write("The whole HTML body explaining whata 404 is??");
}
The problem is that I cannot configure default customErrors
to work with it. When it is on
then it never redirects to the page specified in customErrors
: <error statusCode="404" redirect="404.html"/>
.
I also tried to raise new HttpExcepton(404, "Not Found")
from the handler but then the response code is 200
which I don't understand why.
So the questions are:
- What is the proper way of translating
AnException
into HTTP404
response? - How does
customErrors
section work when handling exceptions in Application_Error? - Why throwing HttpException(404) renders (blank) page with success (200) status?
Thanks,
Dmitriy.