VirtualPathProvider 在 IIS 7.5 的生产中无法(完全)工作
我一直在开发一个具有常见功能的项目,特别是我想共享主文件和相关图像/js/等。为此,母版页及其相关文件都被包装到一个“全局”DLL 中,供所有“子项目”使用。这一切在开发中都非常有效,但部署却带来了一个惊喜,这似乎让很多人措手不及:VirtualPathProvider
在预编译时不起作用。
现在感谢这篇包含解决方法的博客文章我能够再次尝试让它发挥作用。遗憾的是,仍然没有。
我选择放弃我的 Global.asax
实现,并采用博客文章的 AppInitialize
方法:
public static class AppStart
{
public static void AppInitialize()
{
HostingEnvironment hostingEnvironmentInstance = (HostingEnvironment)typeof(HostingEnvironment).InvokeMember("_theHostingEnvironment", BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.GetField, null, null, null);
MethodInfo mi = typeof(HostingEnvironment).GetMethod("RegisterVirtualPathProviderInternal", BindingFlags.NonPublic | BindingFlags.Static);
mi.Invoke(hostingEnvironmentInstance, new object[] { new MasterPageProvider() });
}
}
由于实际的提供程序在调试中工作,因此我不会包含它。如果您想查看它,请随时询问。只是想让问题尽可能简短。
整个情况的有趣之处在于,生产不会产生无法找到母版页的错误。对我来说,这意味着提供程序正在工作,但无论出于何种原因,其余资源(js/css/等)都无法从程序集中正确检索。
所以我的问题归结为:这个解决方案在开发中效果很好,但在 IIS 7.5 上的生产中效果不佳,原因是什么?
更新 11/20/2011
尝试了 David Ebbo 的建议,但不幸的是没有结果。我的网络配置现在看起来像这样:
<configuration>
<connectionStrings>
<clear />
<!-- ... -->
</connectionStrings>
<system.web>
<pages>
<controls>
<!-- ... -->
</controls>
</pages>
<compilation debug="true" targetFramework="4.0" />
<webServices>
<protocols>
<add name="HttpGet"/>
<add name="HttpPost"/>
</protocols>
</webServices>
</system.web>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
</system.webServer>
</configuration>
UPDATE 11/21/2011
为了验证我对 VirtualPathProvider 是否真正工作的怀疑,我注释掉了第三行 (mi.Invoke(.. ..
)并重新部署了该站点,由于无法找到 MasterPage 文件,该问题似乎仅与通过 VPP 传递的静态文件有关。
I have been working on a project that has common bits of functionality, specifically I wanted to share the master file and related images/js/etc. To that end, the master page and its dependent files are all wrapped into a "global" DLL that is utilized by all "subprojects". This all worked great in development, but deployment yielded a surprise which seems to catch a lot of people off guard: VirtualPathProvider
doesn't work when precompiled.
Now thanks to this blog post containing a workaround I was able to give another attempt at getting it to work. Regretfully, it still doesn't.
I opted to get rid of my Global.asax
implementation and went with the blog post's AppInitialize
approach:
public static class AppStart
{
public static void AppInitialize()
{
HostingEnvironment hostingEnvironmentInstance = (HostingEnvironment)typeof(HostingEnvironment).InvokeMember("_theHostingEnvironment", BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.GetField, null, null, null);
MethodInfo mi = typeof(HostingEnvironment).GetMethod("RegisterVirtualPathProviderInternal", BindingFlags.NonPublic | BindingFlags.Static);
mi.Invoke(hostingEnvironmentInstance, new object[] { new MasterPageProvider() });
}
}
Since the actual provider works in debug, I won't include it. If you would like to see it, don't hesitate to ask. Just wanted to keep the question as short as possible.
The interesting aspect to this whole situation is that production yields no errors about not being able to find the master page. To me, this means the provider is working, but for whatever reason the rest of the resources (js/css/etc) aren't being retrieved from the assembly properly.
So my question comes down to this: what are the reasons that this solution would work great in development, but not in production on IIS 7.5?
UPDATE 11/20/2011
Tried out David Ebbo's suggestion and had no results unfortunately. My web config looks something like this now:
<configuration>
<connectionStrings>
<clear />
<!-- ... -->
</connectionStrings>
<system.web>
<pages>
<controls>
<!-- ... -->
</controls>
</pages>
<compilation debug="true" targetFramework="4.0" />
<webServices>
<protocols>
<add name="HttpGet"/>
<add name="HttpPost"/>
</protocols>
</webServices>
</system.web>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
</system.webServer>
</configuration>
UPDATE 11/21/2011
Just to verify my suspicion that the VirtualPathProvider was actually working, I commented out the third line (mi.Invoke(....
) and redeployed the site. As I suspected, it now breaks due to not being able to find the MasterPage file. This issue appears to be related to only static files being delivered through the VPP.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
IIS 7.5 将自行处理静态文件。您需要在 web.config 文件中为每个您希望忽略的静态文件添加一行,以使它们通过您的 VPP 路由。请参阅下面的示例。
IIS 7.5 will handle the static files itself. You need to put a line for each static file you want it to ignore in your web.config file to make them get routed through your VPP. See below for examples.
也许问题在于,默认情况下,IIS 中对静态文件的请求不通过 ASP.NET。
尝试在 web.config 中打开 runAllManagedModulesForAllRequests 是否有帮助。例如
Maybe the problem is that requests for static files are not going through ASP.NET by default in IIS.
Try whether turning on runAllManagedModulesForAllRequests in web.config helps. e.g.
看看这个 发布。它解释了如何通过 IIS 7 中的虚拟路径提供程序获取静态文件。我相信这会解决您的问题。
Take a look at this post. It explains how to get static files through a virtual path provider in IIS 7. I believe this will solve your problem.