来自数据库的视图

r3plica

成员
已加入
2011年12月22日
留言内容
11
编程经验
5-10
所以,
我设法从数据库中获取我的内容以供查看。
现在我的问题是,它们仅以表中的HTML形式(没有布局)被拉回。

谁能向我解释为什么?

这是我的控制器:

C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CMS.Components;
using Core;
using Core.Components;
using CMS;
using System.Collections.ObjectModel;


namespace Cogs.Controllers
{
    [Authorize]
    [ProfileFilter]
    public class PageController : Controller
    {


        #region Properties


        IProfile oProfile
        {
            get { return (IProfile)Session["oProfile"]; }
        }


        #endregion


        //
        // GET: /Pages/


        public ActionResult Index(string url)
        {
            Collection<BasePage> oPages = PageManager.getAllPages(oProfile.CompanyId, false);


            BasePage p = oPages.SingleOrDefault(page => page.Path.ToLower().Equals(url.ToLower()));
 
            if (p != null)
            {
                return View(System.IO.Path.GetFileNameWithoutExtension(p.Path));
            }
            else
            {
                return View();
            }
        }


        public ActionResult View(int Id)
        {
            Page oPage = new Page(Id, oProfile.CompanyId);
            return View(System.IO.Path.GetFileNameWithoutExtension(oPage.Path));
        }
    }
}

这是我的PathProvider:

C#:
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
    {
        public override bool FileExists(string virtualPath)
        {
            BasePage oPage = FindPage(virtualPath);
            if (oPage == null)
            {
                return base.FileExists(virtualPath);
            }
            else
            {
                return true;
            }
        }


        public override VirtualFile GetFile(string virtualPath)
        {
            BasePage oPage = FindPage(virtualPath);
            if (oPage == null)
            {
                return base.GetFile(virtualPath);
            }
            else
            {
                return new PageVirtualFile(virtualPath, oPage.ViewData.ToArray());
            }
        }


        public override VirtualDirectory GetDirectory(string virtualDir)
        {
            return base.GetDirectory(virtualDir);
        }


        private BasePage FindPage(string virtualPath)
        {
            Collection<BasePage> oPages = new Collection<BasePage>();


            if (HttpContext.Current.Session != null && HttpContext.Current.Session["Pages"] == null)
            {
                Collection<BasePage> oAll = PageManager.getAllPages("53AF0033-4011-4C8F-A14D-7CE9301E264D", false);
                HttpContext.Current.Session.Add("Pages", oAll);
            }


            if (HttpContext.Current.Session != null)
            {
                oPages = (Collection<BasePage>)HttpContext.Current.Session["Pages"];
            }


            BasePage oPage = oPages.SingleOrDefault(page => page.ActualPath.ToLower() == virtualPath.ToLower());


            return oPage;
        }
    }
}

这是VirtualFile

C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Hosting;
using System.IO;


namespace CMS
{
    class PageVirtualFile : VirtualFile
    {
        private byte[] data;


        public PageVirtualFile(string virtualPath, byte[] data) : base (virtualPath)
        {
            this.data = data;
        }


        public override System.IO.Stream  Open()
        {
            return new MemoryStream(data);
        }
    }
}

如果让任何人满意,这就是我注册我的PathProvider的地方:

C#:
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();


            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);


            HostingEnvironment.RegisterVirtualPathProvider(new PageVirtualPathProvider());
        }

和往常一样,我们将提供任何帮助。
/ r3plica
 

r3plica

成员
已加入
2011年12月22日
留言内容
11
编程经验
5-10
大家好,


我正在尝试使用MVC框架创建CMS。
一切进展顺利,我创建了表来存储ViewData,ViewTitle和virtualPath。
我使用VirthPathProvider,它看起来像这样:


C#:
    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<dynamic>等等,但是我仍然遇到同样的问题。


导航到虚拟页面时,出现以下错误:
无法将类型为“ ASP._Page_Guidelines_index_cshtml”的对象转换为类型为“ System.Web.IHttpHandler”的对象。


请帮我;我已经坚持了三个星期。
 
最佳 底部