I built a http module. I copied into the GAC . Then, I put into the WebConfig file lke this :


<add name="MyAssembly" type="MyAssembly.Global"/>

this my DDL code :

using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using Microsoft.SharePoint;

namespace MyAssembly{

    public class Global :IHttpModule{

        public void Init(HttpApplication context){
            context.PreRequestHandlerExecute += new EventHandler(context_PreRequestHandlerExecute);    
        public  void Dispose(){


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

        protected void PagePreInit(object sender, EventArgs e){
            using (SPSite site = new SPSite(SPContext.Current.Web.Url)){

                Page page = sender as Page;
                string pageNo = ConfigurationManager.AppSettings["MasterPageNo"];

                if (page != null){

                    if (pageNo.Equals("1")){

                        page.MasterPageFile = "~masterurl/custom.master";
                        if (SPContext.Current != null){

                            SPContext.Current.Web.CustomMasterUrl = "/_catalogs/masterpage/GeneraliTeamSite.MasterPage.Master";
                    else if (pageNo.Equals("2")){
                        page.MasterPageFile = "~masterurl/custom.master";
                        if (SPContext.Current != null){
                            SPContext.Current.Web.CustomMasterUrl = "/_catalogs/masterpage/GeneraliTeamSiteWiki.MasterPage.Master.master";

                        page.MasterPageFile = "~masterurl/default.master";
                        if (SPContext.Current != null){
                            SPContext.Current.Web.MasterUrl = "/_catalogs/masterpage/default.master";


DLL name is: MyAssembly the class is: Global

But It doesn't work!

2 Answers 2


it should be on the lines of:

<add name='MyHttpModule' type=MyAssembly.Global, MyAssembly, Version=, Culture=neutral, PublicKeyToken=87b3480442bff091' />

name = give it a name;

type = namespace.classname , namespace

version = is the version found in project -> properties -> application -> assembly information

culture = neutral

publickeyToken = get the dll from bin folder and drag drop to assebly, find the dll right click, select properties and you should see the key. copy that and paste it in.

this can be achived through activating the feature like so:

class ErrorRedirectHttpModule : SPFeatureReceiver
    #region-- Private Global fields --

        private const string HttpModuleName = "CustomErrorModule";


    /// <summary>
    /// add to the web.config all the the settings like url redirect, safe controls and httpmodule
    /// </summary>
    /// <param name="properties"></param>
    public override void FeatureActivated(SPFeatureReceiverProperties properties)
                    SPWebService mService = SPWebService.ContentService;

                    SPWebConfigModification httpModule = new SPWebConfigModification();
                    httpModule.Owner = HttpModuleName;
                    httpModule.Name = "add[@name='ErrorRedirectHttpModule']";
                    httpModule.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
                    httpModule.Path = "configuration/system.web/httpModules";
                    httpModule.Sequence = 0;
                    httpModule.Value = "<add name='MyHttpModule' type=MyAssembly.Global, MyAssembly, Version=, Culture=neutral, PublicKeyToken=87b3480442bff091' />";

when you deactivate the feature you do the following:

    /// <summary>
    /// run when deactivating feature
    /// </summary>
    /// <param name="properties"></param>
    public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        using (SPSite site = SPContext.Current.Site)
            site.WebApplication.FileNotFoundPage = "";

        var webApp = properties.Feature.Parent as SPWebApplication;
        if (webApp != null)
            RemoveWebConfigModificationsByOwner(webApp, HttpModuleName);

private void RemoveWebConfigModificationsByOwner(SPWebApplication webApp)
   Collection<SPWebConfigModification> modificationCollection = webApp.WebConfigModifications;
   Collection<SPWebConfigModification> removeCollection = new Collection<SPWebConfigModification>();
   int count = modificationCollection.Count;
   for (int i = 0; i < count; i++)
      SPWebConfigModification modification = modificationCollection[i];
      // collect modifications to delete

hope it helps :)

  • Where is the code RemoveWebConfigModificationsByOwner?
    – Tiago
    Commented Aug 8, 2019 at 19:56
  • updated an old post ;)
    – Ali Jafer
    Commented Aug 19, 2019 at 12:18

I've recently worked with the HttpModule also, basically I had a module class and feature and on feature activate and deactivate I wrote the configuration to the web.config file, it is very practical and way to go.

Here is the example how my web.config with my module looks like:

<modules runAllManagedModulesForAllRequests="true">
  <add name="MyHttpModule" type="sharepoint2010.AppFolder.MyHttpModule, sharepoint2010, Version=, Culture=neutral, PublicKeyToken=1eadc186835db154" />

So basically what I see your registration is not totaly right you should provide full assambly name.

For example my classe is called: MyHttpModule The type is: assambley.folder.classname, assambley, version, public token

If you would do it by the feature you could do as follows:

SPWebApplication webApp = properties.Feature.Parent as SPWebApplication;
SPWebConfigModification modification = new SPWebConfigModification("add[@name='MyHttpModule']", "configuration/system.webServer/modules");
modification.Sequence = 0;
modification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
modification.Value = string.Format(@"<add name=""MyHttpModule"" type=""MyAssembly.MyHttpModule, {0}"" />", Assembly.GetExecutingAssembly().FullName);



And as you can see "Assembly.GetExecutingAssembly().FullName" will replace it automatically by the full assembly name.

For the delete you do almost the same but instead of webApp.WebConfigModification.Add you do webApp.WebConfigModification.Remove ...

Hope it was helpfull!

Happy coding!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.