Load HttpModule! Load! HttpModules in App_Code.

I've done HttpModules in separate assemblies and don't remember integration being too tricky. But when I was just writing one within the App_Code of the same site I was having all sorts of issues. I think I found some common gotchas related to HttpModules.

First, my module was not loading but I didn't even get an error page. I was able to get useful errors by switching the App Pool of the site to "Classic .NET AppPool" from the "DefaultAppPool". Turns out this did more than give me error messages, when all was finished I had to have this classic app pool for the module to work. Thanks to lucky abhishek for figuring out the app pool problem first.

Once I was seeing errors I first found that I had other modules in a parent site that could not load. In IIS web.config settings from parent sites are inherited by sites in any virtual sub directories. So I was loading other HttpModules that I didn't care about in this sub site. I fixed this with a quick

<remove name="NameOfModuleIDontHave" />

in the HttpModules section of the web.config in order to straighten that out.

Last I just had to figure out why I "could not load type MYTYPE". Turns out for the type I was using the namespace instead of the class name qualified with the namespace. In other words in my web.config I had

<add type="ParentNamespace.SubNamespace" name="MyClassName" />

instead of

<add type="ParentNamespace.SubNamespace.MyClassName" name="MyClassName" />

As a side note. After I got this working I found for my purposes that the name attribute doesn't mean anything. I can change it to whatever and my sample HttpModule still works.

One last thing I found was that the type attribute above is usually of the form path,assembly. When you're using a class in App_Code you can use the text "App_code" as the assembly if you want to be a little more clear. So this also works:

<add type="ParentNamespace.SubNamespace.MyClassName,App_code" name="MyClassName" />

But in my case it didn't seem to be required.

For reference, this is the skeleton HttpModule I was trying to load.

1 comments: (+add yours?)

Akshay said...

Awesome... solved my problem

Post a Comment