2

Trying to follow this simple example here to load views from a database: http://www.binaryintellect.net/articles/e544d1d3-e47e-4ced-bd4d-8c1eaefbdc31.aspx

The problem is once I register my VirtualPathProdvider my routing no longer gets called.

This is my route config:

  routes.MapRoute(
                "order", // Route name
                "order/{manufacturerid}/{id}/{viewname}/{orderGuid}/{type}", // URL with parameters
                new { controller = "Receipts", action = "order", viewname = "", manufacturerid = "", type = "", orderGuid = "", id = UrlParameter.Optional } // Parameter defaults
               );

I know this because as soon as I register the VirtualPathProvider I no longer see the breakpoint I have set on my ActionResult getting hit that was previously.

Instead, I get a 404 error.

Here is my VirtualPathProvider

public class DatabaseVirtualPathProvider : VirtualPathProvider
    {
        private readonly Utility.Templates.Interfaces.ITemplateService templateService;

        public DatabaseVirtualPathProvider()
        {
            this.templateService = new Utility.Templates.TemplateService(new Utility.Templates.DataAccess.TemplateRepository());
        }

        public override bool FileExists(string virtualPath)
        {
            var view = GetViewFromDatabase(virtualPath);

            if (view == null)
            {
                return base.FileExists(virtualPath);
            }
            else
            {
                return true;
            }
        }

        public override VirtualFile GetFile(string virtualPath)
        {
            var view = GetViewFromDatabase(virtualPath);

            if (view == null)
            {
                return base.GetFile(virtualPath);
            }
            else
            {
                byte[] content = ASCIIEncoding.ASCII.GetBytes(view.TemplateContent);

                return new DatabaseVirtualFile(virtualPath, content);
            }
        }

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

        private Utility.Templates.Models.Template GetViewFromDatabase(string virtualPath)
        {

            var paths = virtualPath.Split('/');
            if (paths.Length < 3) return null;

            var templateName = paths[4];
            var template = templateService.GetByName(Utility.Templates.Models.Enums.TemplateType.Order, templateName);
            return template;
        }
    }

FileExists does get called on my VirtualpathProvider and I do get the template I need from the database but it seems my route is skipped and GetFile never gets called.

Slee
  • 27,498
  • 52
  • 145
  • 243
  • Is the `VirtualPathProvider` registered before you register the routes in `global.asax.cs`? –  Oct 12 '16 at 01:06
  • that was the first thing I tried, before, after, makes no difference I still get 404. FileExists does get called and I do get the template I need from the database but it seems my route is skipped. – Slee Oct 12 '16 at 01:10
  • Check if your virtual path is forming correctly with the parameters – progrAmmar Oct 12 '16 at 02:49
  • the virtual path is forming correctly – Slee Oct 12 '16 at 11:09

0 Answers0