配置 IIS7 通过 ASP.NET 运行时服务器静态内容
我搜索了高低,仍然找不到明确的答案。
如何配置 IIS 7.0 或 IIS 中的 Web 应用程序,以便 ASP.NET 运行时处理所有请求 - 包括对 *.js
、等静态文件的请求>*.gif
等?
我想做的事情如下。
我们有一种 SaaSy 网站,我们可以为每个客户“打造品牌”。 “品牌化”意味着开发自定义母版页并使用一堆 *.css
和其他图像。
很自然地,我使用 VirtualPathProvider
,其操作如下:
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;
}
基本思想如下:我们的 web 应用程序中有一个 branding
文件夹,该文件夹又包含用于每个“品牌”,“品牌”等于主机名。也就是说,对 http://foo.example.com/
的请求应使用 branding/foo_example_com
中的静态文件,而 http://bar.example. com/
应使用来自 branding/bar_example_com
的内容。
现在,我希望 IIS 做的是将所有对静态文件的请求转发给 StaticFileHandler
,然后它会使用整个“基础设施”并提供正确的文件。但是,尽我所能,我无法配置 IIS 来执行此操作。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果应用程序池的
托管管道模式
设置为默认的集成
,II7 就已经这样做了。在集成模式下,ASP.NET 处理所有请求,包括静态对象的请求。如果您必须将应用程序池保留在经典模式下,那么您需要使用与 IIS 6 中使用的相同技术来显式为各种静态扩展创建处理程序。
基于评论的附加信息:我认为您缺少的部分是创建一个
HttpHandler
来处理其他扩展(.js、.css 等)。如果没有这个,ASP.NET 将使用默认处理这些类型的文件。您将在 web.config 中创建对处理程序的引用。 本文是为静态文件创建 HttpHandler 的示例。II7 already does that if the application pool's
Managed Pipeline Mode
is set toIntegrated
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.向每个人致敬,但问题出在完全不同的领域。
VirtualPathProvider
不能在预编译的网站中使用。我很愤怒。Kudos to everyone, but the problem was in totally different space.
VirtualPathProvider
cannot be used in a pre-compiled web site. I'm furious.