加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net – IIS中的自定义虚拟路径提供程序

发布时间:2020-12-16 07:31:26 所属栏目:asp.Net 来源:网络整理
导读:在IIS 7.5中实现自定义虚拟路径提供程序的正确配置是什么?使用ASP.NET Development Server从Visual Studio运行时,以下代码按预期工作,但从IIS运行时不加载映像. .NET 4.0项目文件 CustomVirtualPathProvider.zip – SkyDrive文件 Web.config文件 ?xml versi
在IIS 7.5中实现自定义虚拟路径提供程序的正确配置是什么?使用ASP.NET Development Server从Visual Studio运行时,以下代码按预期工作,但从IIS运行时不加载映像.

.NET 4.0项目文件

CustomVirtualPathProvider.zip – SkyDrive文件

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,@"CrazyImagePath",_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;
    }
}

文件系统

CrazyImagePathBox.png

IIS配置

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

解决方法

以下是我发现的“修复”我的问题.

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()});

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读