0

In global.asax we have the possibility to implement methods that corresponds to an application event, for example Application_EndRequest, and add whatever code we want to these.

I'm developing an plugin that have the need to attach to some of these events, is there any way to programmatically push actions for these into the application flow somehow?

The goal is obviously to avoid the need for manually adding code in global.asax when using the plugin.

Alex
  • 14,104
  • 11
  • 54
  • 77

1 Answers1

2

How to hook an HTTP Module into an MVC application

If I understand you correctly, you want to create an external library and "hook" it up to an MVC application's events.

1. Create a simple class library.

The first thing is to create a simple class library. We'll call it TestLib.

Creating TestLib

2. Create a new class called TestLibHttpModule.

The class implements IHttpModule. This grants it the Init() method. This method will be called when the module is initialised and it passes us the HttpApplication object that is initialising the module.

In our Init method, we'll attach a new EventHandler to the EndRequest event.

For now, our event handler method will simply throw an exception with a cheeky message.

namespace TestLib
{
    public class TestLibHttpModule : IHttpModule
    {
        public void Dispose()
        {

        }

        public void Init(HttpApplication context)
        {
            context.EndRequest += new EventHandler(context_EndRequest);
        }

        private void context_EndRequest(object sender, EventArgs e)
        {
            // get the HttpApplication context
            HttpApplication context = (HttpApplication)sender;
            throw new NotImplementedException("At least it works.");
        }
    }
}

3. Point the library's build path to our MVC project.

Assuming you have an MVC project already set up nearby, perhaps called TestApp, point the build path of your library to be the bin folder of your MVC project. Now, every time we build the module, it'll be thrown into the MVC project.

Changing the build path

4. Update MVC project to use the HttpModule.

The Web.config of an MVC application has a spot for specifying Http Modules. Under the system.Web element, add a new httpModules section (if it doesn't already exist).

<system.web>
[ ... ]
    <httpModules>
        <add name="TestLibModule" type="TestLib.TestLibHttpModule, TestLib" />
    </httpModules>
</system.web>

5. Run the MVC application.

Running the app

Rowan Freeman
  • 15,724
  • 11
  • 69
  • 100
  • Ah, excellent answer about implementing it as a HttpModule. I might add that the configuration for IIS7+ in slightly different: http://stackoverflow.com/a/2935410/681538. And while playing around, I also noticed that we are not allowed to attach and detach event handlers dynamically: *Event handlers can only be bound to HttpApplication events during IHttpModule initialization*. So considering this, I think it might be appropriate to set this answer as accepted, even though it bypasses the actual question of adding event listeners dynamically somehow. – Alex Jul 08 '14 at 11:18
  • Ah, ok. Sorry about that; I wasn't entirely sure if I got the question right. That's interesting about IIS. I'm running IIS 7.5 and my technique works for me. – Rowan Freeman Jul 08 '14 at 23:07
  • Well, I had both a specific question and a goal, you answered my goal where it seems like the specific question is a negative. So I'm all in. Strange that your web.config seems to work in IIS7.5, but at least any consumers of this answer now have two options. – Alex Jul 09 '14 at 02:30