SharePoint and HTTP Module

In this article I would like to take you through deploying an HTTP module in SharePoint 2013.

Scenario

Redirect users from accessing the Site Contents page (viewlsts.aspx).

Procedure

Open Visual Studio and create a Class Library project.

Add a reference to the System.Web library.

System web

Replace the class file with the following code where:

  • Class implements the IHttpModule interface
  • Redirection when URL contains viewlsts.aspx

 

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using System.Web;  
  7. using System.Web.UI;  
  8. namespace SPModule  
  9. {  
  10.     public class SPModule : IHttpModule  
  11.     {  
  12.         public void Dispose() { }  
  13.         public void Init(HttpApplication context) { context.PreRequestHandlerExecute += context_PreRequestHandlerExecute; }  
  14.         void context_PreRequestHandlerExecute(object sender, EventArgs e)  
  15.         {  
  16.             HttpApplication app = sender as HttpApplication;  
  17.             if (app != null)  
  18.             {  
  19.                 Page page = app.Context.CurrentHandler as Page;  
  20.                 if (page != null) { if (HttpContext.Current.Request.Url.AbsoluteUri.Contains("_layouts/15/viewlsts.aspx")) { HttpContext.Current.Response.Redirect("http://www.bing.com"); } }  
  21.             }  
  22.         }  
  23.     }  
  24. }  
Build the code and copy the DLL to the IIS SharePoint BIN folder as shown below.

SP Module

Now, open the web.config and find the modules section. (Please note that there is another httpModules section that is not for our purpose.)

html code

Add your class name in the format Assembly.ClassName. Save the changes.

Refresh your SharePoint site and try accessing the viewlsts.aspx page using the URL given below:

http://server/_layouts/15/viewlsts.aspx

You should see the page is redirected to www.bing.com.

Bing

References

HTTP Module

Summary

In this article we saw how to use an HTTP Module within SharePoint. The source code is attached with the article.

Up Next
    Ebook Download
    View all
    Learn
    View all