Question

I have custom IHttpModule to redirect users to my 404 aspx page.

This example was found somewhere in the internet and this part looks like working well but today administrators reports next problem:

Hi   Has been seen on this today and have found out. It turns out that it's http module "PageNotFound" that troble it for WebDAV when saving from Office documents. It adds an http 302 redirect when to store documents. This happens only if the document does not exist, works fine if you save over an existing document. Can be tested to remove / add "PageNotFoundHttpModule" from web.config WebDAV works fine.

 void ApplicationPreSendRequestContent(object sender, EventArgs e)
        {
            HttpResponse response = app.Response;
            string requestedUrl = app.Request.Url.AbsolutePath;

            if (string.IsNullOrEmpty(requestedUrl))
            {
                LoggingService.WriteError("PageNotFoundHttpModule. PreSend request: HttpApplication Request.Url.AbsolutePath is null ");
                return;
            }

            try
            {
                var urlElements = requestedUrl.Split('/');
                if (urlElements.Length > 0)
                {
                    int pageElementIndex = urlElements.Length - 1;
                    string pageName = urlElements[pageElementIndex] as string;

                    if (string.IsNullOrEmpty(pageName))
                    {
                        pageName = string.Empty;
                    }

                    if (response.StatusCode == 404 && response.ContentType.Equals("text/html", StringComparison.CurrentCulture))
                    {
                        var pathLowercase = Constants.PageNotFoundUrl.ToLower().Trim();
                        var message = string.Format("{0}?oldUrl={1}&k={2}", pathLowercase, requestedUrl, pageName);
                        if (!pathLowercase.EndsWith(pageName.ToLower().Trim()))
                        {
                            response.Redirect(message);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LoggingService.WriteError(ex, "PageNotFoundHttpModule. Exception while working");
            }
        }

I checked logs and i see next exception:

    PageNotFoundHttpModule. Exception while working  Thread was being aborted.  
   at System.Threading.Thread.AbortInternal()     
at System.Threading.Thread.Abort(Object stateInfo)     
at System.Web.HttpResponse.End()     
at PageNotFoundHttpModule.ApplicationPreSendRequestContent(Object sender, EventArgs e).

But I really dont know what is gooing wrong with this thread and why its Aborted. Any ideas?

Was it helpful?

Solution 2

We should check that HTTPmethod is not profined. And only then use redirect.

app.Request.HttpMethod != "PROPFIND"

OTHER TIPS

This is the natural behaviour of HttpContext

When you try to redirect the page, the current thread will be aborted and the system will throw an exception.

If you don't want the thread to abort, pass false to redirect method.

response.Redirect(message, false);

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top