Sep 19, 2009

Redirect the page using HTTP module

Many times we need to redirect the page to some custom page based on some processing,this custom page can be some error page or any other page.So to achieve this we are using HttpModule and HttpHandler.

Steps :

Steps :
1) Open visual studio
2) Create a new project of type class library
3) Add sharepoint reference to it
4) Inherit the class from “IHttpModule” class

Follow this code:

---------------------------------------------
namespace MyModule
{
public class LogoutModule : IHttpModule //inherit from the class
{
public void Init(HttpApplication context)
{
// Create a event handler to execute the process
context.EndRequest += new EventHandler(context_EndRequest);
}

void context_EndRequest(object sender, EventArgs e)

{

Page page = HttpContext.Current.CurrentHandler as Page;

if (HttpContext.Current.Request.Url.Equals("http://spvm:1212/Pages/Default.aspx"))

{

HttpContext.Current.Response.Redirect("http://spvm:1212/sites/dms/Pages/Default.aspx");

}

}

}

}

----------------------------------------------------

To execute the HttpModule it should be registered in the Web.Config file of site and dll in the GAC.

This is the entry in the web.config file

Under <httpModules>tag Section

----------------------------------------------------

<add type="ClassName,AssemblyName, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9f4da00116c38ec5" />

-------------------------

Your job is done..

Enjoy!!!

No comments:

Post a Comment