views:

481

answers:

2

I searched high an low and still cannot find a definite answer.

How do I configure IIS 7.0 or a Web Application in IIS so that ASP.NET Runtime will handle all requests -- including ones to static files like *.js, *.gif, etc?

What I'm trying to do is as follows.

We have kind of SaaSy site, which we can "brand" for every customer. "Branding" means developing a custom master page and using a bunch of *.css and other images.

Quite naturally, I'm using VirtualPathProvider, which operates like this:

public override System.Web.Hosting.VirtualFile GetFile(string virtualPath)
{
    if(PhysicalFileExists(virtualPath))
    {
        var virtualFile = base.GetFile(virtualPath);
        return virtualFile;
    }

    if(VirtualFileExists(virtualPath))
    {
        var brandedVirtualPath = GetBrandedVirtualPath(virtualPath);
        var absolutePath = HttpContext.Current.Server.MapPath(brandedVirtualPath);

        Trace.WriteLine(string.Format("Serving '{0}' from '{1}'", 
            brandedVirtualPath, absolutePath), "BrandingAwareVirtualPathProvider");

        var virtualFile = new VirtualFile(brandedVirtualPath, absolutePath);
        return virtualFile;    
    }

    return null;
}

The basic idea is as follows: we have a branding folder inside our webapp, which in turn contains folders for each "brand", with "brand" being equal to host name. That is, requests to http://foo.example.com/ should use static files from branding/foo_example_com, whereas http://bar.example.com/ should use content from branding/bar_example_com.

Now what I want IIS to do is to forward all requests to static files to StaticFileHandler, which would then use this whole "infrastructure" and serve correct files. However, try as I might, I cannot configure IIS to do this.

+4  A: 

II7 already does that if the application pool's Managed Pipeline Mode is set to Integrated which is the default. In Integrated mode, ASP.NET handles all requests including those for static objects.

If you have to leave your application pool in Classic Mode then you need to use the same techniques you would use in IIS 6 to explicitly create handlers for the various static extensions.

Additional Information Based on Comments: I think your missing piece is creating an HttpHandler to handle the other extensions (.js, .css, etc.). Without this, then ASP.NET will use the default handling for these types of files. You would create a reference to you handler in your web.config. This article is an example of creating an HttpHandler for static files.

Keltex
See my edits. Does your answer still holds true in light of this?
Anton Gogolev
+1  A: 

Kudos to everyone, but the problem was in totally different space.

VirtualPathProvider cannot be used in a pre-compiled web site. I'm furious.

Anton Gogolev