26

I am trying to register a custom HttpHandler in the web.config file. MSDN's example shows an entry that is commented out...um which doesn't work so well. When I uncomment I receive a Could not load file or assembly 'HttpModule.cs' or one of its dependencies. The system cannot find the file specified. error. The file name is HttpModule.cs and the class name is MyHttpModule. There is no explicit namespace yet.

<httpModules>
     <add name="MyHttpModule" type="MyHttpModule, HttpModule" />
<httpModules>

I am sure I am overlooking the obvious. Do I need to specify the file path somewhere? Thanks in advance.

Praesagus
  • 2,029
  • 5
  • 30
  • 48

6 Answers6

23

I am still learning much of the terminology and needed it spelled out for me. In case any of you need the same...

As an example if:

  • The base class name and project name is TestSite
  • The namespace my class is in is BusinessLogic
  • The class name is PageAuthorization

in the Web.config file...

<configuration> 
 <system.web> 
  <httpModules> 
   <add type= "BusinessLogic.PageAuthorization, TestSite" name="PageAuthorization" /> 
  </httpModules> 
 </system.web> 
</configuration>

In my case I had to mark my class with IHttpModule. The definition of the class would look like:

public class PageAuthorization : IHttpModule
Praesagus
  • 2,029
  • 5
  • 30
  • 48
20

If you are loading Custom module from .Net 4.0 GAC and IIS 6/7 classic mode you have to use below code

 <system.web>
  <httpModules>
  <add name="ClientHttpModule" type="ClientServHttpModule.ClientHttpModule,ClientServHttpModule,Version=1.0.0.0, Culture=neutral, PublicKeyToken=3af8d8e2f9e8b6c3" />
      </httpModules>
 </system.web>

ClientServHttpModule is namespace of your custom module. Public key token you can get it from sn.exe app.

if you are running in Integrated mode.use below code

<system.webServer>
        <modules>
            <add name="ClientHttpModule" type="ClientServHttpModule.ClientHttpModule,ClientServHttpModule,Version=1.0.0.0, Culture=neutral, PublicKeyToken=3af8d8e2f9e8b6c3" />
        </modules>
    </system.webServer>
Rishi
  • 219
  • 3
  • 3
16

The type value is in the format of {Class}, {assembly}.

So in your case, it should be MyHttpModule, MyDllName

Where MyDllName is the name of the compiled DLL.

Nalaka526
  • 11,278
  • 21
  • 82
  • 116
Mitchel Sellers
  • 62,228
  • 14
  • 110
  • 173
6

Note for people landing here looking for configuration for IIS 7 and above running in Integrated Mode:

<configuration> 
 <system.webServer> 
  <modules> 
   <add type= "BusinessLogic.PageAuthorization, TestSite" name="PageAuthorization" /> 
  </modules> 
 </system.webServer> 
</configuration>
Sudhanshu Mishra
  • 6,523
  • 2
  • 59
  • 76
  • @Rishi I could use help with configuring system.webserver. If any of you guys can help please see my new post here at http://stackoverflow.com/questions/27354083/web-config-system-webserver-errors – te7 Dec 08 '14 at 10:25
3

To add to this, it is important to be sure that the .dll you are trying to use is in the project bin folder. I had the problem of making my module in a separate project, but forgetting to push over the .dll file.

kachingy123
  • 197
  • 1
  • 12
0

I think HttpModules property in system.web is for ASP 3.5 or before. For ASP 4 or above use modules in system.webserver:

<system.webServer>
    <modules>
        <add name="TestModule"  type="WebApplication1.TestModule, WebApplication1"/>
    </modules>
    <validation validateIntegratedModeConfiguration="false"/>
</system.webServer>