我创建了一个VirtualPathProvider,允许基于Request.Host移动自定义文件夹中的所有资源,以使用一个Web应用程序管理多个域。
在我的本地计算机上MVC应用程序工作,当我发布到Azure时,我获得了一个不明确的错误(HTTP 503),我不明白我要检查什么以获得更多信息。
这里是提供者的实现:
public class PerSiteVirtualPathProvider : VirtualPathProvider
{
private readonly IDictionary<string, string> domainToPath;
public PerSiteVirtualPathProvider(IDictionary<string, string> domainToPath)
{
this.domainToPath = domainToPath;
}
public override bool FileExists(string virtualPath)
{
return base.FileExists(virtualPath) || PerSiteFileExists(virtualPath);
}
public override bool DirectoryExists(string virtualDir)
{
return base.DirectoryExists(virtualDir) || PerSiteFileExists(virtualDir);
}
public override string GetFileHash(string virtualPath, IEnumerable virtualPathDependencies)
{
if (PerSiteFileExists(virtualPath))
{
var host = HttpContext.Current.Request.Url.Host.ToLower();
return string.Concat(host, "/", virtualPath);
}
return base.GetFileHash(virtualPath, virtualPathDependencies);
}
public override VirtualFile GetFile(string virtualPath)
{
if (PerSiteFileExists(virtualPath))
{
var physicalPath = CombinePath(virtualPath);
return new PhysicalVirtualFile(virtualPath, physicalPath);
}
return base.GetFile(virtualPath);
}
public override CacheDependency GetCacheDependency(string virtualPath, IEnumerable virtualPathDependencies, DateTime utcStart)
{
if (PerSiteFileExists(virtualPath))
{
var host = HttpContext.Current.Request.Url.Host.ToLower();
return new CacheDependency(new[] { CombinePath(virtualPath) }, new[] { host });
}
return Previous.GetCacheDependency(virtualPath, virtualPathDependencies, utcStart);
}
private string CombinePath(string virtualPath)
{
var host = HttpContext.Current.Request.Url.Host.ToLower();
var cleanVirtualPath = virtualPath.TrimStart('~', '/').Replace('/', '\\');
if (!domainToPath.ContainsKey(host))
{
throw new InvalidOperationException(string.Format("The specified '{0} domain is not registered.", host));
}
var domainFolder = domainToPath[host];
var path = Path.Combine(domainFolder, cleanVirtualPath);
return path;
}
private bool PerSiteFileExists(string virtualPath)
{
var path = CombinePath(virtualPath);
var fileExists = File.Exists(path);
return fileExists;
}
}
答案 0 :(得分:2)
我解决了这个问题,我使用了App_Data目录来读取VirtualPathProvider不允许的所有资源,请参阅here,特别是在备注段落中:
您无法存储ASP.NET应用程序文件夹或在虚拟文件系统中生成应用程序级程序集的文件。这包括: