Redirect OSSSearchResults.aspx to another page

As promised yesterday, here’s some prototype code to redirect calls to OSSSearchResults.aspx to another page:

namespace SearchRedirector
{
    public class HTTPModule : IHttpModule
    {
        public void Init(HttpApplication context)
        {
            context.PreRequestHandlerExecute += new EventHandler(RegisterPreInitRequestHandler);
        }

        void RegisterPreInitRequestHandler(object sender, EventArgs e)
        {
            Page page = HttpContext.Current.CurrentHandler as Page;
            if (page != null)
            {
                page.PreInit += new EventHandler(page_PreInit);
            }
        }

        void page_PreInit(object sender, EventArgs e)
        {
            Page page = sender as Page;
            if (page != null)
            {
                if (page.Request.Url.AbsolutePath.Contains("OSSSearchResults.aspx"))
                {
                    page.Response.Redirect("/SearchCenter/Pages/results.aspx" + page.Request.Url.Query , true);
                }
            }
        }
        public void Dispose()
        {
        }
    }
}

This seems to work pretty well, although for some reason it’s tricky getting the debugging in Visual Studio to break into the code working consistently.

The guts of this is in the page_PreInit function, where we’re checking to see if the page is the OSS Search results, and if so, we redirect, passing the appropriate query string params.

Obviously, for a production system you’ll need to add a lot more configuration around this – what page(s) we’re forwarding to, what context(s) we should forward for, and so on. There are probably more efficient ways of checking if the page is an OSS results page than a string Contains() too.

Advertisements
Redirect OSSSearchResults.aspx to another page

2 thoughts on “Redirect OSSSearchResults.aspx to another page

  1. Dan says:

    i’m trying to read between the lines and am failing miserably. Where exactly does this code go?

  2. Hi Dan – It’s an HTTPModule, so it’s compiled to an assembly, and that put in the GAC. You need to then add an entry into web.config – there is a section about HTTPModules.

    Reading around HTTPModules should help – Good ASP.NET books should cover this. Also, this is a good overview

    http://msdn.microsoft.com/en-us/library/bb398986.aspx

    There is an example with that too:
    http://msdn.microsoft.com/en-us/library/ms227673(VS.80).aspx

    It’s always worth remembering the ASP application life cycle
    http://msdn.microsoft.com/en-us/library/ms178473.aspx

    Remember – this is ASP.NET 2.0 functionality, not SharePoint. Your Module will process (and modify) the inbound request before it arrives with SharePoint.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s