private void HandleBeginRequest(object sender, EventArgs e)
        {
            var application      = (HttpApplication)sender;
            var context          = application.Context;
            var relativeFilePath = context.Request.AppRelativeCurrentExecutionFilePath.TrimStart('~');

            // Check whether the current request URL contains information about a preview mode
            if (HandleVirtualContext(ref relativeFilePath))
            {
                // Validate integrity of preview information (including the unique identifier generated by Kentico) in the current request URL
                if (!VirtualContext.ValidatePreviewHash(relativeFilePath) || !ValidatePreviewGuid(VirtualContext.GetItem(VirtualContext.PARAM_WF_GUID)))
                {
                    VirtualContext.Reset();
                    throw new HttpException(404, "The preview link is not valid.");
                }

                // Disable same origin policy for a preview mode as Kentico displays preview in a frame
                AntiForgeryConfig.SuppressXFrameOptionsHeader = true;

                // Do not cache response in a preview mode, it has to contain current data
                context.Response.Cache.SetNoServerCaching();
                context.Response.Cache.SetNoStore();

                // Remove preview mode information from the request URL
                context.RewritePath("~" + relativeFilePath, context.Request.PathInfo, context.Request.Url.Query.TrimStart('?'));
            }

            var previewFeature = new PreviewFeature();

            context.Kentico().SetFeature <IPreviewFeature>(previewFeature);
        }
 /// <summary>
 /// Handles the virtual context for the request.
 /// </summary>
 /// <param name="relativePath">Relative path. If loading succeeded, returns updated virtual path without the context values.</param>
 /// <returns>True, if loading of the virtual context succeeded and the request is a virtual context request, otherwise false.</returns>
 /// <exception cref="HttpException">When virtual context is invalid.</exception>
 private static bool HandleVirtualContext(ref string relativePath)
 {
     try
     {
         return(VirtualContext.HandleVirtualContext(ref relativePath));
     }
     catch (InvalidVirtualContextException ex)
     {
         VirtualContext.Reset();
         throw new HttpException(404, "The preview link is not valid.", ex);
     }
 }