Configure IIS7 to server static content through ASP.NET Runtime

风格不统一 提交于 2019-12-07 11:21:03

问题


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.


回答1:


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.




回答2:


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

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



来源:https://stackoverflow.com/questions/2593932/configure-iis7-to-server-static-content-through-asp-net-runtime

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!