コード例 #1
0
        public virtual void Handle(HttpContextBase context)
        {
            if (context == null)
            {
                return;
            }

            if (IsNotFoundException(context.Server.GetLastError(), context.Request.Url))
            {
                context
                .ClearServerError()
                .SetStatusCode(404);
                _requestHandler.Handle(context);
            }
        }
コード例 #2
0
        public virtual void Handle(HttpContextBase context)
        {
            if (context == null)
            {
                return;
            }

            if (IsHandled(context))
            {
                LogDebug("Already handled.", context);
                return;
            }

            if (context.Response.StatusCode != 404)
            {
                LogDebug("Not a 404 response.", context);
                return;
            }

            if (_configuration.FileNotFoundHandlerMode == FileNotFoundMode.Off)
            {
                LogDebug("Not handled, custom redirect manager is set to off.", context);
                return;
            }
            // If we're only doing this for remote users, we need to test for local host
            if (_configuration.FileNotFoundHandlerMode == FileNotFoundMode.RemoteOnly)
            {
                // Determine if we're on localhost
                var localHost = IsLocalhost(context);
                if (localHost)
                {
                    LogDebug("Determined to be localhost, returning.", context);
                    return;
                }
                LogDebug("Not a localhost, handling error.", context);
            }

            LogDebug("Handling 404 request.", context);

            var notFoundUri = context.Request.Url;

            if (IsResourceFile(notFoundUri))
            {
                LogDebug("Skipping resource file.", context);
                return;
            }

            var query = context.Request.ServerVariables["QUERY_STRING"];

            // avoid duplicate log entries
            if (query != null && query.StartsWith("404;"))
            {
                LogDebug("Skipping request with 404; in the query string.", context);
                return;
            }

            var canHandleRedirect = HandleRequest(context.Request.UrlReferrer, notFoundUri, out var newUrl);

            if (canHandleRedirect && newUrl.State == (int)RedirectState.Saved)
            {
                LogDebug("Handled saved URL", context);

                context
                .ClearServerError()
                .RedirectPermanent(newUrl.NewUrl);
            }
            else if (canHandleRedirect && newUrl.State == (int)RedirectState.Deleted)
            {
                LogDebug("Handled deleted URL", context);

                SetStatusCodeAndShow404(context, 410);
            }
            else
            {
                LogDebug("Not handled. Current URL is ignored or no redirect found.", context);

                SetStatusCodeAndShow404(context);
            }

            MarkHandled(context);
        }
コード例 #3
0
 public virtual void SetStatusCodeAndShow404(HttpContextBase context, int statusCode = 404)
 {
     context
     .ClearServerError()
     .SetStatusCode(statusCode);
 }
コード例 #4
0
        public virtual void Handle(HttpContextBase context)
        {
            if (context == null)
            {
                return;
            }

            if (context.Response.StatusCode != 404)
            {
                LogDebug("Not a 404 response.", context);
                return;
            }

            LogDebug("Handling 404 request.", context);

            var notFoundUri = context.Request.Url;

            if (IsResourceFile(notFoundUri))
            {
                LogDebug("Skipping resource file.", context);
                return;
            }

            var query = context.Request.ServerVariables["QUERY_STRING"];

            // avoid duplicate log entries
            if (query != null && query.StartsWith("404;"))
            {
                LogDebug("Skipping request with 404; in the query string.", context);
                return;
            }

            var redirectManagerRepository = new RedirectManagerRepository();
            var redirect = redirectManagerRepository.Get(notFoundUri?.AbsoluteUri);

            if (redirect != null)
            {
                LogDebug("Handled URL", context);
                var newUrl = redirect.NewUrl.Trim();

                if (!newUrl.Contains("http"))
                {
                    newUrl = Path.Combine(GetDefaultWebSite(), newUrl);
                }

                if (redirect.IncludeQuery && context.Request.Url != null)
                {
                    newUrl = newUrl + context.Request.Url.Query;
                }

                Logger.Debug("New absolute Url: " + newUrl);

                if (redirect.Type == 301)
                {
                    context
                    .ClearServerError()
                    .RedirectPermanent(newUrl);
                }
                else
                {
                    context
                    .ClearServerError()
                    .Redirect(newUrl);
                }
            }
            else
            {
                LogDebug("Not handled. Current URL is ignored or no redirect found.", context);

                context
                .ClearServerError()
                .SetStatusCode(404);
            }
        }