Custom Virtual Path Provider in IIS

亡梦爱人 提交于 2019-12-03 16:18:29
Adam Carr

Here is what I found to "fix" my issue.

http://sunali.com/2008/01/09/virtualpathprovider-in-precompiled-web-sites/

In brief:

HostingEnviornment explicitly ignores Virtual Path Providers in precompiled sites. You can circumvent this limitation by using reflection to call an internal version that omits this check. Thus, instead of calling

HostingEnviornment.RegisterVirtualPathProvider(new EmbeddedViewVirtualPathProvider();

call this instead:

typeof(HostingEnvironment).GetMethod("RegisterVirtualPathProviderInternal",
      BindingFlags.Static | BindingFlags.InvokeMethod | BindingFlags.NonPublic)
    .Invoke(null, new object[] {new EmbeddedViewPathProvider()});

I had the same problem but Tom Clarkson led me on the right track, he's absolutely right in that you need additional configuration in order to make IIS serve the content provider by the virtual path provider. I found a solution here

Here is an example web.config-snippet that I think will work for you

<system.web>
  <httpHandlers>
    <add path="*.png" verb="*" type="System.Web.StaticFileHandler" validate="true" />
    <add path="*.gif" verb="*" type="System.Web.StaticFileHandler" validate="true" />
  </httpHandlers>
</system.web>

You could also register a "wildcard httphandler" under a special location (eg "/MyVirtualFiles"), which might be useful if your virtual path provider serves many different file types.

<location path="MyVirtualFiles">
  <system.web>
    <httpHandlers>
      <add path="*" verb="*" type="System.Web.StaticFileHandler" validate="true" />
    </httpHandlers>
  </system.web>
</location>

When FileExists returns true, it is interpreted as "there is a file there, IIS can serve it without ASP.NET". To get the next step of actually downloading the file to go through your virtual path provider, you need to set IIS to use ASP.NET to serve all image files and add code in global.asax or an http handler that will make use of your virtual path provider.

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