I had to implement a custom HttpModule to handle a 404 error in Sharepoint.
It listens for the PreSendRequestContent event, and looks for a 404 status code. If one is found it does a TransferRequest.
void App_PreSendRequestContent(object sender, EventArgs e)
{
HttpResponse res = App.Response;
HttpRequest req = App.Request;
if (res.StatusCode == 404 && !req.Url.AbsolutePath.Equals(PageNotFoundUrl, StringComparison.InvariantCultureIgnoreCase))
{
App.Server.TransferRequest(PageNotFoundUrl);
}
}
This works just fine, but I noticed in Fiddler that the page is showing a 200 status code, even though the original request was a 404. This is not good for search engines.
Is this an expected behaviour of TransferRequest? Can I somehow maintain the 404 status code? Or, would I have been better off using a good old fashioned Server.Transfer?
Update
I tried this outside of a sharepoint environment, and the Server.TransferRequest request does indeed give a 200 status code, removing the 404. Server.Transfer doesn't work as I don't think it can given the pipeline.
Update 2
Thanks to the answer below, I have added the following:
void App_PostRequestHandlerExecute(object sender, EventArgs e)
{
HttpResponse res = App.Response;
HttpRequest req = App.Request;
if (req.Url.AbsolutePath.Equals(PageNotFoundUrl, StringComparison.InvariantCultureIgnoreCase))
{
res.StatusCode = 404;
}
}
An HTTP status code is a server response to a browser's request. When you visit a website, your browser sends a request to the site's server, and the server then responds to the browser's request with a three-digit code: the HTTP status code.
The HTTP 201 Created success status response code indicates that the request has succeeded and has led to the creation of a resource.
The HTTP 200 OK success status response code indicates that the request has succeeded. A 200 response is cacheable by default. The meaning of a success depends on the HTTP request method: GET : The resource has been fetched and is transmitted in the message body.
Well, TransferRequest() triggers a new request, which implies a new response. Since the resource that PageNotFoundUrl
points to does exist, the client receives a legitimate 200 OK
status header.
You might want to write an HTTP handler (or handle an event in Global.asax) in order to force the status header to 404 Not Found
when serving PageNotFoundUrl
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With