Exemplo n.º 1
0
        /// <summary>
        /// Tries to find and assign an Umbraco document to a <c>PublishedContentRequest</c>.
        /// </summary>
        /// <param name="contentRequest">The <c>PublishedContentRequest</c>.</param>
        /// <returns>
        /// A value indicating whether an Umbraco document was found and assigned.
        /// </returns>
        /// <remarks>
        /// Optionally, can also assign the template or anything else on the document request, although that is not required.
        /// </remarks>
        public bool TryFindContent(PublishedContentRequest contentRequest)
        {
            if (contentRequest == null)
            {
                throw new ArgumentNullException(nameof(contentRequest));
            }
            try
            {
                // Hand over to 404 page configured for IIS, including the requested URL in the format used by IIS
                var errorUrl = new HttpStatusFromConfiguration().GetCustomUrlForStatusCode(404);
                if (errorUrl != null)
                {
                    var requestUrl = contentRequest.RoutingContext.UmbracoContext.HttpContext.Request.Url;
                    contentRequest.RoutingContext.UmbracoContext.HttpContext.Server.TransferRequest(errorUrl + "?404;" + HttpUtility.UrlEncode(requestUrl.ToString()));
                }
            }
            catch (Exception e)
            {
                e.ToExceptionless().Submit();
            }

            // If we get this far, returning false displays the "this page is intentionally ugly" Umbraco 404.
            // Returning true causes a 500 "There is no PublishedContent."
            return(false);
        }
        private void Context_EndRequest(object sender, EventArgs e)
        {
            // Handle requests that resulted in a 404
            var notFoundUrl = new HttpStatusFromConfiguration().GetCustomUrlForStatusCode(404);

            if (notFoundUrl != null && HttpContext.Current != null && HttpContext.Current.Response.StatusCode == 404)
            {
                // If a URL was requested that would not have been handled by Umbraco, pass it to the configured 404 page.
                // Check for the 404 page itself because the request for the 404 comes back here and needs to be ignored.
                var pathRequested              = HttpContext.Current.Request.Url.AbsolutePath;
                var extensionRequested         = VirtualPathUtility.GetExtension(pathRequested);
                var extensionsHandledByUmbraco = new List <string> {
                    string.Empty, ".aspx"
                };
                if (!pathRequested.StartsWith(notFoundUrl.ToString()) && !extensionsHandledByUmbraco.Contains(extensionRequested))
                {
                    HttpContext.Current.Server.TransferRequest(notFoundUrl + "?404;" + HttpUtility.UrlEncode(HttpContext.Current.Request.Url.ToString()));
                }
            }
        }