Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Server.TransferRequest() and the http status code

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;
    }
}
like image 692
ScottE Avatar asked Feb 14 '11 20:02

ScottE


People also ask

What is an HTTP status code?

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.

What is the HTTP status code 201 indicate?

The HTTP 201 Created success status response code indicates that the request has succeeded and has led to the creation of a resource.

Which of these sends a response HTTP status code 200 to the browser?

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.


1 Answers

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.

like image 99
Frédéric Hamidi Avatar answered Nov 02 '22 04:11

Frédéric Hamidi