0

I have a URL that looks like: /employees?employeeId=3992, which displays the employee with the ID 3992. That works fine and the HTTP Status Code is 200 OK.

However when someone writes an invalid ID in the URL such as: /employees?employeeId=39920, I would like my HTTP Handler to rewrite the path to our error page, and send HTTP Status Code 404.

Here's the code I'm using:

if (employee == null)
{
    context.RewritePath("/error-page");
    context.Response.StatusCode = 404; // if I remove this line, the error-page is displayed, and the StatusCode is 200.
}

When I set the HTTP Status Code to 404, the page displays this standard ASP.NET 404 error page.

enter image description here

Martin
  • 2,302
  • 2
  • 30
  • 42
  • 1
    Check out this post which seems to cover what you're trying to achieve http://helephant.com/2009/02/11/improving-the-way-aspnet-handles-404-requests/ – Luke Baughan Feb 21 '13 at 13:53

1 Answers1

0

Also call the

context.Response.TrySkipIisCustomErrors = true;

eg:

if (employee == null)
{
    context.Response.TrySkipIisCustomErrors = true;
    context.RewritePath("/error-page");
    context.Response.StatusCode = 404; 
}
Aristos
  • 66,005
  • 16
  • 114
  • 150