生产中的Sitecore自定义404处理程序

时间:2013-03-25 01:20:57

标签: c# sitecore

我从Stackoverflow中获取了以下代码 - >博客重新处理Sitecore中的自定义404(实际上将302重定向到404页面,状态为200,谷歌将其作为软件404获取)。

虽然这在我们的本地测试服务器中完全正常工作,但是当我们将其投入生产时,该网站会变得混乱并且需要时间,例如8-9分钟加载和东西。

public class ExecuteRequest : Sitecore.Pipelines.HttpRequest.ExecuteRequest
{
    protected override void RedirectOnItemNotFound(string url)
    {
        var context = System.Web.HttpContext.Current;

        try
        {
            // Request the NotFound page
            var domain = context.Request.Url.GetComponents(
                UriComponents.Scheme | UriComponents.Host, 
                UriFormat.Unescaped);

            var content = WebUtil.ExecuteWebPage(
                string.Concat(domain, url));

            // The line below is required for IIS 7.5 hosted 
            // sites or else IIS is gonna display default 404 page
            context.Response.TrySkipIisCustomErrors = true;
            context.Response.StatusCode = 404;
            context.Response.Write(content);
        }
        catch (Exception ex)
        {
            Log.Error(string.Format("Falling back to default redirection behavior. Reason for error {0}", ex), ex);

            // Fall back to default behavior on exceptions
            base.RedirectOnItemNotFound(url);
        }

        context.Response.End();
    }
} 

P.S:然后我用web.config中的自定义替换了ExecuteRequest。

如果您经历过类似的事情或知道任何问题,请务必说清楚。

提前致谢

4 个答案:

答案 0 :(得分:6)

Sitecore中有一个设置,您可以使用它来摆脱302重定向:

<setting name="RequestErrors.UseServerSideRedirect" value="true" />

使用此设置,网址保持不变,状态代码为404.如果您想要一些额外的逻辑(例如将Sitecore项目显示为错误页面),则{{}上有一个名为Error Manager的共享源模块{3}}

希望有所帮助。

答案 1 :(得分:0)

检查服务器是否能够访问您网站的主机名。

服务器通常无法访问DNS,因此无法解析主机名。为了让404处理程序工作,应用程序需要能够访问自己的主机名来请求404页面。

为了确保这一点,请编辑服务器的hosts文件并在其中添加主机名条目,并将其指向127.0.0.1

答案 2 :(得分:0)

您可以通过创建新的解析器来解决它。当您想以正确的语言提供给用户错误页面时,这是一个很好的解决方案。但是在IIS 7.0和7.5中存在一些差异。

将处理器添加到您的sitecore配置:

<processor type="Sitecore.Pipelines.HttpRequest.ItemResolver, Sitecore.Kernel"/>
<processor type="Project.Error404Resolver, Project" />

处理器解析它:

对于IIS 7.0:

public class Error404Resolver : Sitecore.Pipelines.HttpRequest.HttpRequestProcessor
{
    public override void Process(Sitecore.Pipelines.HttpRequest.HttpRequestArgs args)
    {
        if(Sitecore.Context.Item == null && !args.Context.Request.Url.AbsolutePath.StartsWith("/sitecore")
        {
            args.Context.Response.Clear();

            SiteContext site = Sitecore.Context.Site;
            if(site != null)
            {
                Item item404Page = Sitecore.Context.Database.GetItem(site.RootPath + "website/error/404");
                if(item404Page != null)
                {
                    Sitecore.Context.Item = item404Page;
                    args.Context.Response.StatusCode = (int) System.Net.HttpStatusCode.NotFound;
                }
            }
        }
    }
}

对于IIS 7.5:

public class Error404Resolver : Sitecore.Pipelines.HttpRequest.HttpRequestProcessor
{
    public override void Process(Sitecore.Pipelines.HttpRequest.HttpRequestArgs args)
    {
        if(Sitecore.Context.Item == null && !args.Context.Request.Url.AbsolutePath.StartsWith("/sitecore")
        {
            args.Context.Response.Clear();

            SiteContext site = Sitecore.Context.Site;
            if(site != null)
            {
                Item item404Page = Sitecore.Context.Database.GetItem(site.RootPath + "website/error/404");
                if(item404Page != null)
                {
                    WebClient webClient = new WebClient();
                    webClient.Encoding = args.Context.Request.ContentEncoding;
                    webClient.Headers.Add("User-Agent", args.Context.Request.UserAgent);
                    string page = webClient.DownloadString(LinkManager.GetItemUrl(item404Page));

                    args.Context.Response.StatusCode = (int) System.Net.HttpStatusCode.NotFound;
                    args.Context.Response.Write(page);
                    args.Context.Response.TrySkipIisCustomErrors = true;
                    args.Context.Response.End();
                }
            }
        }
    }
}

使用此功能,您将在当前页面中呈现错误页面而不进行重定向,并返回到浏览器代码404。

答案 3 :(得分:0)

我在目前工作的客户处遇到同样的问题(看起来代码已粘贴),实际上原因很明显:如果您使用未在Sitecore网站配置中注册的网址执行此调用(但通过IIS访问),您还将运行此代码。不幸的是,WebUtil.ExecuteWebPage调用也使用了错误的url执行,因此你最终陷入了循环。

实际上,您应该在日志中看到很多这些消息:回退到默认的重定向行为。错误原因{0} ,可能是超时。

如果您确实想使用自定义处理程序,则应在调用WebUtil.ExecuteWebPage之前检查您是否位于正确的站点上下文中。

相关问题