This blog post explains how you can override the ExecuteRequest processor in the httpRequestBegin pipeline to control how the Sitecore ASP.NET web Content Management System (CMS) handles some kinds of errors.
As specified in the Sitecore Presentation Component API Cookbook linked in the Resource section at the end of this blog post, under certain error conditions, the ExecuteRequest processor in the httpRequestBegin pipeline may redirect or transfer to the error pages specified. For example, if layout details do not specify a layout for the context device for the context item, the ExecuteRequest processor redirects to the URL specified by the LayoutNotFoundUrl setting in the Web.config file. Technically, if the RequestErrors.UseServerSideRedirect setting in the Web.config file is true, Sitecore invokes the System.Web.HttpContext.Current.Server.Transfer() method; otherwise it invokes the System.Web.HttpContext.Current.Response.Redirect() method.
You might want to handle these types of error conditions differently. For example, you might want to log the condition or throw an potentially throw an exception rather than redirecting. If you throw an exception, ASP.NET should handle it according to how you configure the /configuration/system.web/customErrors element in the /web.config file, which you cannot configure with a Sitecore Web.config include file.
To override the default logic for handling such error conditions, you can implement a solution based on the following prototype. For most error conditions, this prototype simply logs a warning and invokes the corresponding logic in the default implementation. When a layout does not exist for an item or the context user does not have read access to the definition item for that layout, this solution logs a warning and throws an exception.
namespace
Sitecore.Sharedsource.Pipelines.HttpRequest
{
using
System;
System.Web;
SC = Sitecore;
public
class
ExecuteRequest : SC.Pipelines.HttpRequest.ExecuteRequest
protected
override
void
RedirectOnLayoutNotFound(
string
url)
SC.Diagnostics.Log.Warn(
this
+
" : No layout for "
+ HttpContext.Current.Request.RawUrl +
" ("
+ Sitecore.Context.User +
" - "
+ Sitecore.Context.Device.Name +
")"
,
);
if
(
true
)
throw
new
Exception(
"No layout."
}
else
base
.RedirectOnLayoutNotFound(url);
RedirectOnItemNotFound(
" : No item for "
+ Sitecore.Context.User.Name +
false
"No item."
.RedirectOnItemNotFound(url);
RedirectOnNoAccess(
" : No access for "
"No access."
.RedirectOnNoAccess(url);
RedirectOnSiteAccessDenied(
" : No site access for "
"No site access."
.RedirectOnSiteAccessDenied(url);
You can use a Web.config include file based on the following (Sitecore.Sharedsource.ExecuteRequest.config in my case) to enable this processor:
<
configuration
xmlns:patch
=
"https://www.sitecore.com/xmlconfig/"
>
sitecore
pipelines
httpRequestBegin
processor
type
"Sitecore.Pipelines.HttpRequest.ExecuteRequest, Sitecore.Kernel"
patch:attribute
name
"type"
>Sitecore.Sharedsource.Pipelines.HttpRequest.ExecuteRequest,Sitecore.Sharedsource</
</