我正在尝试使用 MVC 框架创建一个 CMS。一切进展顺利,我创建了表来存储我的 ViewData、ViewTitle 和 virtualPath。我使用 VirthPathProvider,它看起来像这样:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Hosting;
using CMS;
using System.Collections.ObjectModel;
using CMS.Components;
using System.Web;
namespace CMS.Providers
{
public class PageVirtualPathProvider : VirtualPathProvider
{
private Directory current { get; set; }
private Collection<Directory> directories { get; set; }
private Collection<BasePage> pages { get; set; }
public override bool FileExists(string virtualPath)
{
string Path = (VirtualPathUtility.GetDirectory(virtualPath) != "~/") ? VirtualPathUtility.RemoveTrailingSlash(VirtualPathUtility.GetDirectory(virtualPath)) : VirtualPathUtility.GetDirectory(virtualPath);
if (IsVirtualPath(Path))
{
BasePage oPage = FindPage(virtualPath);
if (oPage != null)
return true;
}
bool bExists = base.FileExists(virtualPath);
return bExists;
}
public override VirtualFile GetFile(string virtualPath)
{
string Path = (VirtualPathUtility.GetDirectory(virtualPath) != "~/") ? VirtualPathUtility.RemoveTrailingSlash(VirtualPathUtility.GetDirectory(virtualPath)) : VirtualPathUtility.GetDirectory(virtualPath);
if (IsVirtualPath(Path))
{
BasePage oPage = FindPage(virtualPath);
if (oPage != null)
return new PageVirtualFile(virtualPath, oPage.ViewData.ToArray());
}
return base.GetFile(virtualPath);
}
public override bool DirectoryExists(string virtualDir)
{
if (IsVirtualPath(virtualDir))
{
if (current != null)
{
if (current.Path.ToLower() != virtualDir.ToLower())
current = new Directory(virtualDir, "53AF0033-4011-4C8F-A14D-7CE9301E264D");
}
else
{
current = new Directory(virtualDir, "53AF0033-4011-4C8F-A14D-7CE9301E264D");
}
if (current != null)
return true;
}
return base.DirectoryExists(virtualDir);
}
public override VirtualDirectory GetDirectory(string virtualDir)
{
if (IsVirtualPath(virtualDir))
{
if (current != null)
{
if (current.Path.ToLower() != virtualDir.ToLower())
current = new Directory(virtualDir, "53AF0033-4011-4C8F-A14D-7CE9301E264D");
}
else
{
current = new Directory(virtualDir, "53AF0033-4011-4C8F-A14D-7CE9301E264D");
}
if (current != null)
return new CmsVirtualDirectory(virtualDir, "53AF0033-4011-4C8F-A14D-7CE9301E264D");
}
return base.GetDirectory(virtualDir);
}
public override System.Web.Caching.CacheDependency GetCacheDependency(string virtualPath, System.Collections.IEnumerable virtualPathDependencies, DateTime utcStart)
{
string Path = (VirtualPathUtility.GetDirectory(virtualPath) != "~/") ? VirtualPathUtility.RemoveTrailingSlash(VirtualPathUtility.GetDirectory(virtualPath)) : VirtualPathUtility.GetDirectory(virtualPath);
if (IsVirtualPath(Path))
return null;
return base.GetCacheDependency(virtualPath, virtualPathDependencies, utcStart);
}
public override string GetFileHash(string virtualPath, System.Collections.IEnumerable virtualPathDependencies)
{
string Path = (VirtualPathUtility.GetDirectory(virtualPath) != "~/") ? VirtualPathUtility.RemoveTrailingSlash(VirtualPathUtility.GetDirectory(virtualPath)) : VirtualPathUtility.GetDirectory(virtualPath);
if (IsVirtualPath(Path))
return Guid.NewGuid().ToString();
return base.GetFileHash(virtualPath, virtualPathDependencies);
}
private BasePage FindPage(string virtualPath)
{
string VirtualName = VirtualPathUtility.GetFileName(virtualPath).ToLower();
if (pages == null)
{
pages = PageManager.getAllPages("53AF0033-4011-4C8F-A14D-7CE9301E264D", false);
}
BasePage oPage = pages.SingleOrDefault(page => page.Path.ToLower() == VirtualName);
return oPage;
}
private bool IsVirtualPath(string virtualPath)
{
string Path = VirtualPathUtility.ToAppRelative(virtualPath);
if (directories == null)
{
directories = DirectoryManager.GetAllDirectories("53AF0033-4011-4C8F-A14D-7CE9301E264D");
}
Directory oDir = directories.SingleOrDefault(dir => dir.Path.ToLower() == Path.ToLower());
if (oDir == null || virtualPath == "~/") return false; // If we don't have directory, return false
return true; // Hit only if we are null
}
}
}
现在我的问题是:获取页面很好,当它们是虚拟的时,它返回 null 作为 Cache 并且 FileHash 总是一个不同的字符串,所以调用 GetFile 。我的文件被返回,但它永远找不到布局。
我相信这是因为没有 _ViewStart.cshtml 因为没有视图目录....那么我该如何强制它使用布局?我已经尝试了很多东西,比如让我的虚拟页面从@inherits System.Web.Mvc.WebViewPage 继承等,但我仍然遇到同样的问题......
当我导航到虚拟页面时,出现此错误:无法将“ASP._Page_Guidelines_index_cshtml”类型的对象转换为“System.Web.IHttpHandler”类型。
请帮我; 我已经坚持了3周......