Http modules are called on every request when using the mvc / routing module

I am developing an http module that connects to the FormsAuthentication module via the Authenticate event.

While debugging, I noticed that the module (and all other registered modules) gets hit every time the client requests a resource (also when it requests images, stylesheets, javascript files (etc.)). This happens both when running on an IIS 7 server in integrated pipeline mode, and when debugging via a webdev server (in non-integrated pipeline mode)

Since I am developing a website with a lot of images that are not normally cached by the client browser, it will hit modules at a lot of awkward times.

I am using MVC and its routing engine (System.Web.Routing.UrlRoutingModule). When a new site is created, the runAllManagedModulesForAllRequests attribute for the IIS 7 section (system.webServer) is set to true by default in the web.config file, which, as the name suggests, forces it to invoke all modules on every single request.

If the runAllManagedModulesForAllRequests attribute is false, no modules will be called.

It looks like the reason for this is because the routing module or mvc (not sure why exactly), which results in the asp.net (aspx) handler never being called, and therefore events and modules are never called (once, as expected).

I tested this by trying to name "mydomain.com/Default.aspx" instead of "mydomain.com/" and correctly it only calls modules once as it is supposed to.

How can I fix this so it only calls modules once when a page is requested and also when all other resources are requested?

Is there a way to register that all requests should run an asp.net (aspx) handler, except for requests for specific file type extensions? Of course, this is not a problem if I want to go with urls like / content / images / myimage 123 for images (no extension). But I don't think of another way to fix it.

Is there a better way to solve this problem?

I tried to set up ignoreRoute like this route.IgnoreRoute ("content / {* pathInfo}"); where the content folder contains all images, javascripts and stylesheets in seperat subfolders, but doesn't seem to change anything.

I can see many different possibilities when setting up the handler, but I cannot figure out how one can configure it so that it can use the routing module and have urls like / blog / post 123 and not call modules when requesting images, javascripts and stylesheets (etc.).

Hope someone out there can help me?

Martin

+2


source to share


2 answers


The problem is the routing module.



The solution is to move images, css, js to a subdomain, or perhaps you can register file / extension types that the routing module should ignore.

0


source


The following code is what I use in every MVC application to avoid the overhead caused by the routing system when serving static files, javascript, css, etc:



public static void RegisterRoutes(RouteCollection routes)
{
    routes.RouteExistingFiles = false;
    routes.LowercaseUrls = true;
    routes.AppendTrailingSlash = true;
    routes.IgnoreRoute("Content/{*pathInfo}");
    routes.IgnoreRoute("Scripts/{*pathInfo}");
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    routes.IgnoreRoute("{*favicon}", new { favicon = @"(.*/)?favicon.ico(/.*)?" });

    /* ... */
}

      

0


source







All Articles