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.
i’m trying to read between the lines and am failing miserably. Where exactly does this code go?
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.