尝试从我的数据库加载视图时,我的路由不会被调用

时间:2016-10-12 00:46:30

标签: c# asp.net-mvc asp.net-routing

尝试在此处使用此简单示例从数据库加载视图: http://www.binaryintellect.net/articles/e544d1d3-e47e-4ced-bd4d-8c1eaefbdc31.aspx

问题是,一旦我注册了VirtualPathProdvider,我的路由就不再被调用了。

这是我的路线配置:

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

我知道这一点,因为只要我注册VirtualPathProvider,我就不会再看到我在ActionResult上设置的断点被点击之前。

相反,我收到404错误。

这是我的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确实调用了{p> VirtualpathProvider,我确实从数据库中获取了我需要的模板,但似乎我的路线被跳过,GetFile永远不会被调用。

0 个答案:

没有答案