4

I have an http module on a sharepoint site and this module instantiates a custom class and add it to the session and does other initial things for my site. However, I'm noticing that the http module is being called for all request types (.aspx, .js, .png, .jpg).

Is there any way to have an http module only be called for .net specific page types?

Alex Angas
  • 59,219
  • 41
  • 137
  • 210

5 Answers5

0

Here is some simple example how to filter requests by extension... the example below exclude from the processing files with the specific extensions.

public class AuthenticationModule : IHttpModule
{
    private static readonly List<string> extensionsToSkip = AuthenticationConfig.ExtensionsToSkip.Split('|').ToList();

    // In the Init function, register for HttpApplication 
    // events by adding your handlers.
    public void Init(HttpApplication application)
    {
        application.BeginRequest += new EventHandler(this.Application_BeginRequest);
        application.EndRequest += new EventHandler(this.Application_EndRequest);
    }

    private void Application_BeginRequest(Object source, EventArgs e)
    {
        //  we don't have to process all requests...
        if (extensionsToSkip.Contains(Path.GetExtension(HttpContext.Current.Request.Url.LocalPath)))
            return;

        Trace.WriteLine("Application_BeginRequest: " + HttpContext.Current.Request.Url.AbsoluteUri);
    }

    private void Application_EndRequest(Object source, EventArgs e)
    {
        //  we don't have to process all requests...
        if (extensionsToSkip.Contains(Path.GetExtension(HttpContext.Current.Request.Url.LocalPath)))
            return;

        Trace.WriteLine("Application_BeginRequest: " + HttpContext.Current.Request.Url.AbsoluteUri);
    }
}

In config file specify what extensions should be excluded and initiate the list of extensions in the module.

Mr. Pumpkin
  • 6,212
  • 6
  • 44
  • 60
0

In IIS you will set up the handler to be associated with your specific extension so the handler will only be applied to that extension. JavaScript files should not be processed.

rmontgomery429
  • 14,660
  • 17
  • 61
  • 66
0

While I do like the ease of deployment of this type of http handler (and the fact that you do not have to deploy a web.config entry for the handler), in cases where you may not want to use the _layouts directory OR you want to have a custom file extension, here is an alternative method that works as well (although it does take one manual configuration step in IIS so it may not be suitable for a "No Touch Deployment")

1) Create your http handler as you normally would for an asp.net application. You can add references to the SharePoint DLLs and interact with the object model since you are in the App Pool.

2) Add and entry into your web.config to register your handler and define the extension you are going to use. IE:

3) Define your custom extension in IIS through the IIS > Web SIte Properties > Home Directory > Configuration > Mappings

In this case, we defined a .proxy extension that the handler will pick up. Our handler is a .NET assembly so we need to add the mapping to route .proxy requests to the .net isapi dll (C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll).. also, make sure you UNcheck the "

From comments on http://msdn.microsoft.com/en-us/library/bb457204.aspx

rmontgomery429
  • 14,660
  • 17
  • 61
  • 66
0

I've done a bit more research and it seems there is no way to do what I'm intending. I will have to check the request type and cancel from there.

desertnaut
  • 57,590
  • 26
  • 140
  • 166
0

You can do this in a very lightweight manner using a HttpModule (before making any calls to the expensive SharePoint object model) by checking the extension in the content of the last Uri.Segments

void context_BeginRequest(object sender, EventArgs e)
{
    HttpApplication app = (HttpApplication)sender;
    Uri uri = app.Request.Url;
    string lastSegment = uri.Segments[uri.Segments.Length-1];
    .. check your extension here an do nothing if it doesn't match.
    ..
}

We use this in our 'TinyURL' implementation for SharePoint to ensure the performance impact for regular URLs is almost 0.

Jeroen Ritmeijer
  • 2,772
  • 3
  • 24
  • 31