Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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

<httpModules>
  ....

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

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";
                        }

                    }
                    else{
                        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!

share|improve this question
add comment

2 Answers

it should be on the lines of:

<add name='MyHttpModule' type=MyAssembly.Global, MyAssembly, Version=1.0.0.0, 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";

    #endregion

    /// <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=1.0.0.0, Culture=neutral, PublicKeyToken=87b3480442bff091' />";
                    mService.WebConfigModifications.Add(httpModule);
                    mService.Update();
                    mService.ApplyWebConfigModifications();
     }
 }

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 = "";
            site.WebApplication.Update(true);
        }

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

hope it helps :)

share|improve this answer
add comment

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=1.0.0.0, Culture=neutral, PublicKeyToken=1eadc186835db154" />
</modules>

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);

webApp.WebConfigModifications.Add(modification);
webApp.Update();

webApp.WebService.ApplyWebConfigModifications();

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!

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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