我创建了一个 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;
}
}