VirtualPathProvider doesn't (quite) work in production on IIS 7.5

末鹿安然 提交于 2020-01-13 05:48:08

问题


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.


回答1:


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.

<system.webServer>
    <handlers>
        <add name="Images" path="*.png" verb="GET,HEAD,POST" type="System.Web.StaticFileHandler" modules="ManagedPipelineHandler" resourceType="Unspecified" />
        <add name="Stylesheets" path="*.css" verb="GET,HEAD,POST" type="System.Web.StaticFileHandler" modules="ManagedPipelineHandler" resourceType="Unspecified" />
     </handlers>
 </system.webServer>



回答2:


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.

<modules runAllManagedModulesForAllRequests="true" />



回答3:


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.



来源:https://stackoverflow.com/questions/8163058/virtualpathprovider-doesnt-quite-work-in-production-on-iis-7-5

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