2

I wrote a VirtualPathProvider to dynamically load my MVC views, which are saved in a database. The problem is that it is taking too long to load the views because the class ViewPathProvider is getting all paths and searching for them in the database: "/PrecompiledApp.config", "~/_appstart.cshtml", "~/_appstart.vbhtml" "/Views/Home/Index.aspx", "/Views/Home/Index.ascx", "/Views/Shared/Index.aspx" (...) and many others. Is there a way to define a list of paths to reduce time and the number of queries made against the db?

My Global.asax.cs

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            RouteConfig.RegisterRoutes(RouteTable.Routes);         
            HostingEnvironment.RegisterVirtualPathProvider(new ViewPathProvider());
        } 

    My VirtualPathProvider.cs

        public class ViewPathProvider : VirtualPathProvider
        {
            public override bool FileExists(string virtualPath)
            {
                var page = FindPage(virtualPath);
                if (page == null)
                {
                    return base.FileExists(virtualPath);
                }
                else
                {
                    return true;
                }
            }

            public override VirtualFile GetFile(string virtualPath)
            {
                var page = FindPage(virtualPath);
                if (page == null)
                {
                    return base.GetFile(virtualPath);
                }
                else
                {
                    return new MyVirtualFile(virtualPath, page);
                }
            }

            public override CacheDependency GetCacheDependency(string virtualPath,    IEnumerable virtualPathDependencies, DateTime utcStart)
            {
                return null;
            }

            public override string GetFileHash(string virtualPath, IEnumerable virtualPathDependencies)
            {
                return Guid.NewGuid().ToString();
            }

            private string FindPage(string virtualPath)
            {
                string filetype = virtualPath.Substring(virtualPath.Length - 5);
                if(filetype != ".aspx" && filetype != ".ascx" && filetype != "bhtml")
                {
                    Uri host = System.Web.HttpContext.Current.Request.Url;
                    string hostName = host.Host.ToString();
                    Company company = new Company();
                    CMSComponent companiesComponent = new CMSComponent();
                    company = companiesComponent.GetCompanyDetailsByCmsUrl(hostName, virtualPath);
                    Debug.Print(virtualPath);
                    if (company.CompanyId!=0)
                    {                    
                        return company.cmsHtml;
                    }
                    else
                    {
                        return null;
                    }
                }
                else
                {
                    return null;
                }
            }
        }

     Thanks!

0 Answers0