Configuring IIS7 for static server content through ASP.NET Runtime

I was looking for a high low and still can not find a specific answer.

How to configure IIS 7.0 or a web application in IIS so that ASP.NET Runtime processes all requests, including static ones, for example, *.js , *.gif , etc.

I am trying to do the following.

We have a SaaSy website that we can "strum" for each client. β€œBranding” means developing a custom homepage and using a bunch of *.css and other images.

It is only natural that I use VirtualPathProvider , which works as follows:

 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 main idea is this: we have a branding folder inside our webapp, which, in turn, contains folders for each "brand", and the "brand" is equal to the host name. That is, requests from http://foo.example.com/ should use static files from branding/foo_example_com , while http://bar.example.com/ should use content from branding/bar_example_com .

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

+4
source share
2 answers

II7 already does this if the Managed Pipeline Mode application pool is set to Integrated , which is the default value. In integrated mode, ASP.NET processes all requests, including for static objects.

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

Additional information based on comments . I think your missing piece creates an HttpHandler to handle other extensions (.js, .css, etc.). Without this, ASP.NET will use the default processing for these file types. You would create a link to the handler in your web.config. This article is an example of creating an HttpHandler for static files.

+5
source

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

VirtualPathProvider cannot be used on a precompiled website. I am angry.

+1
source

Source: https://habr.com/ru/post/1306262/


All Articles