【问题标题】:Custom Virtual Path Provider in IISIIS 中的自定义虚拟路径提供程序
【发布时间】:2011-05-13 05:30:27
【问题描述】:

在 IIS 7.5 中实现自定义虚拟路径提供程序的正确配置是什么?以下代码在使用 ASP.NET 开发服务器从 Visual Studio 运行时按预期工作,但从 IIS 运行时不加载图像。

.NET 4.0 项目文件

CustomVirtualPathProvider.zip - 网盘文件

Web.config

<?xml version="1.0"?>
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.webServer>
     <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
</configuration>

Default.aspx

<%@ Page Title="Home Page" Language="C#" AutoEventWireup="true" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Virtual Path Provider</title>
    </head>
    <body>
        <img src="Box.png" />
    </body>
</html>

Global.asax

public class Global : System.Web.HttpApplication
{
    void Application_Start(object sender, EventArgs e)
    {
        System.Web.Hosting.HostingEnvironment.RegisterVirtualPathProvider(new WebApplication1.CustomVirtualPathProvider());
    }
}

CustomVirtualFile.cs

public class CustomVirtualFile : System.Web.Hosting.VirtualFile
{
    private string _VirtualPath;

    public CustomVirtualFile(string virtualPath) : base(virtualPath)
    {
        _VirtualPath = virtualPath.Replace("/", string.Empty);
    }

    public override Stream Open()
    {
        string ImageFile =
            System.IO.Path.Combine(HttpContext.Current.Request.PhysicalApplicationPath, @"Crazy\Image\Path", _VirtualPath);
        return System.IO.File.Open(ImageFile, FileMode.Open, FileAccess.Read);
    }
}

CustomVirtualPathProvider.cs

public class CustomVirtualPathProvider : System.Web.Hosting.VirtualPathProvider
{
    Collection<string> ImageTypes;

    public CustomVirtualPathProvider() : base()
    {
        ImageTypes = new Collection<string>();
        ImageTypes.Add(".PNG");
        ImageTypes.Add(".GIF");
    }

    public override bool FileExists(string virtualPath)
    {
        if (IsImage(virtualPath))
        {
            return true;
        }
        return base.FileExists(virtualPath);
    }

    public override System.Web.Hosting.VirtualFile GetFile(string virtualPath)
    {
        if (IsImage(virtualPath))
        {
            return new CustomVirtualFile(virtualPath);
        }
        return base.GetFile(virtualPath);
    }

    private bool IsImage(string file)
    {
        return ImageTypes.IndexOf(file.ToUpperInvariant().Substring(file.Length - 4, 4)) > -1;
    }
}

文件系统

\Crazy\Image\Path\Box.png

IIS 配置

没有配置更改的默认站点。

【问题讨论】:

  • 您是否尝试过调试和破解您的代码?是否正在调用 Application_Start,您的 CustomVirtualPathProvider 构造函数或其任何方法呢?

标签: asp.net iis-7.5


【解决方案1】:

这是我发现“解决”我的问题的方法。

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

简而言之:

HostingEnviornment 明确忽略预编译站点中的虚拟路径提供程序。您可以通过使用反射调用省略此检查的内部版本来规避此限制。因此,而不是调用

HostingEnviornment.RegisterVirtualPathProvider(new EmbeddedViewVirtualPathProvider();

改为调用它:

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

【讨论】:

  • 这意味着......它还不是一个答案。请改为添加评论:)
【解决方案2】:

我遇到了同样的问题,但 Tom Clarkson 带领我走上了正确的道路,他完全正确,因为您需要额外的配置才能使 IIS 通过虚拟路径提供程序服务于内容提供程序。我找到了解决方案here

这是一个我认为对你有用的示例 web.config-sn-p

<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>

您还可以在特殊位置(例如“/MyVirtualFiles”)下注册“通配符 httphandler”,如果您的虚拟路径提供程序服务于许多不同的文件类型,这可能会很有用。

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

【讨论】:

    【解决方案3】:

    当 FileExists 返回 true 时,它​​被解释为“那里有一个文件,IIS 可以在没有 ASP.NET 的情况下提供它”。要获得实际下载文件以通过虚拟路径提供程序的下一步,您需要将 IIS 设置为使用 ASP.NET 来提供所有图像文件,并在 global.asax 或 http 处理程序中添加代码,以使用您的虚拟路径提供程序。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-03
      • 1970-01-01
      • 2011-07-08
      • 2021-09-10
      • 2012-09-16
      • 2012-12-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多