Server.TransferRequest()和HTTP状态code [英] Server.TransferRequest() and the http status code

查看:194
本文介绍了Server.TransferRequest()和HTTP状态code的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不得不实现一个自定义的HttpModule在Sharepoint来处理404错误。

据侦听preSendRequestContent事件,并寻找404状态code。如果找到它确实TransferRequest。

 无效APP_ preSendRequestContent(对象发件人,EventArgs的发送)
{
    HTT presponse解析度= App.Response;
    HTT prequest REQ = App.Request;    如果(res.Status code == 404安培;&安培;!req.Url.AbsolutePath.Equals(PageNotFoundUrl,StringComparison.InvariantCultureIgnoreCase))
    {
        App.Server.TransferRequest(PageNotFoundUrl);
    }
}

这工作得很好,但我在小提琴手注意到,页面呈现出200状态code,即使原来的请求是404。这是不好的搜索引擎。

这是一个预期的行为<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.httpserverutility.transferrequest%28v=VS.90%29.aspx\"相对=nofollow> TransferRequest ?我能以某种方式维持404状态code?或者,我会更好过使用良好的老式Server.Transfer的?

更新

我试过这个SharePoint环境之外,和Server.TransferRequest要求确实给200状态code,删除,因为我不认为它可以赋予你的404 Server.Transfer的不工作管道。

更新2

由于下面的答案,我已经添加了以下内容:

 无效App_PostRequestHandlerExecute(对象发件人,EventArgs的发送)
{
    HTT presponse解析度= App.Response;
    HTT prequest REQ = App.Request;    如果(req.Url.AbsolutePath.Equals(PageNotFoundUrl,StringComparison.InvariantCultureIgnoreCase))
    {
        res.Status code = 404;
    }
}


解决方案

好了, TransferRequest()触发一个新的请求,这意味着新的响应。由于资源的 PageNotFoundUrl 点的确实存在的,客户端会收到一个合法的 200 OK 状态标题。

您可能要编写一个HTTP处理程序(或处理全球的事件。 ASAX ),以迫使地位头 404未找​​到发球时 PageNotFoundUrl

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()和HTTP状态code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆