Views from the database

r3plica

Member
Joined
Dec 22, 2011
Messages
11
Programming Experience
5-10
So,
I have managed to pull my content for a view from the database.
Now my issue is that they are being pulled back only as the HTML in the table (without it's layout).

Can anyone explain to me why?

here is my Controller:

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

and here is my 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;
        }
    }
}

and this is the 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);
        }
    }
}

and if it pleases anyone, this is my where i register my PathProvider:

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


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


            HostingEnvironment.RegisterVirtualPathProvider(new PageVirtualPathProvider());
        }

as usual, any help is appreciated....
/r3plica
 

r3plica

Member
Joined
Dec 22, 2011
Messages
11
Programming Experience
5-10
Hi all,


I am trying to create a CMS using the MVC framework.
All was going well, I created my tables to store my ViewData, ViewTitle and virtualPath.
I use VirthPathProvider and it looks like this:


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
            }
        }
    }



Now my problem is this:
Getting the pages is fine, when they are virtual it returns null as Cache and the FileHash is always a different string, so GetFile is called.
My file is returned, but it never finds the Layout.


I believe this is because there is no _ViewStart.cshtml because there is no views directory....So how can I force it to use a Layout?
I have tried so many things, like getting my virtual pages to inherit from @inherits System.Web.Mvc.WebViewPage<dynamic>, etc, but I still get the same problem....


When I navigate to a virtual page, I get this error:
Unable to cast object of type 'ASP._Page_Guidelines_index_cshtml' to type 'System.Web.IHttpHandler'.


Please help me; I have been stuck on this for 3 weeks....
 
Top Bottom