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;
}
}
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
.
Server.TransferRequest
doesn't change the status code. It passes control of the request on to another page without telling the client. You can manually set the status code in your custom 404 page in the Page_Load
event:
protected void Page_Load(object sender, System.EventArgs e)
{
Response.StatusCode = 404;
Response.StatusDescription = "Not Found";
}
链接地址: http://www.djcxy.com/p/50848.html
上一篇: 测试TFS2010构建模板的最简单方法