Example #1
0
        public void StartRazorHost(string projectFolder)
        {
            StopRazorHost();

            var host = new RazorFolderHostContainer <KavaDocsTemplate>()
            {
                // *** Set your Folder Path here - physical or relative ***
                TemplatePath = Path.GetFullPath(Path.Combine(projectFolder, "_kavadocs", "themes")),
                // *** Path to the Assembly path of your application
                BaseBinaryFolder = Environment.CurrentDirectory
            };

            // Add any assemblies that are referenced in your templates
            host.AddAssemblyFromType(typeof(RazorTemplates)); // Dochound
            host.AddAssemblyFromType(typeof(StringUtils));    // Westwind.Utilities
            host.AddAssemblyFromType(typeof(Markdig.Markdown));
            host.AddAssemblyFromType(typeof(Newtonsoft.Json.JsonConvert));
            host.AddAssemblyFromType(typeof(MarkdownMonster.App));

            host.ReferencedNamespaces.Add("DocHound");
            host.ReferencedNamespaces.Add("DocHound.Model");
            host.ReferencedNamespaces.Add("DocHound.Utilities");
            host.ReferencedNamespaces.Add("DocHound.Configuration");
            host.ReferencedNamespaces.Add("Westwind.Utilities");

            // Always must start the host
            host.Start();

            RazorHost = host;
        }
Example #2
0
        public void RuntimeErrorWithExceptionTest()
        {
            var host = new RazorFolderHostContainer();

            host.ThrowExceptions = true;

            host.TemplatePath     = Path.GetFullPath(@"..\..\FileTemplates\");
            host.BaseBinaryFolder = Environment.CurrentDirectory;

            // add model assembly - ie. this assembly
            host.AddAssemblyFromType(typeof(Person));
            host.UseAppDomain = false;

            // these are implicitly set
            host.Configuration.CompileToMemory = false;
            //host.Configuration.TempAssemblyPath = Environment.CurrentDirectory;

            host.Start();

            Person person = new Person()
            {
                Name    = "Rick",
                Company = "West Wind",
                Entered = DateTime.Now,
                Address = new Address()
                {
                    Street = "32 Kaiea",
                    City   = "Paia"
                }
            };

            bool exception = false;

            try
            {
                string result = host.RenderTemplate("~/RuntimeError.cshtml", person);
            }
            catch (RazorHostContainerException ex)
            {
                Console.WriteLine(ex.InnerException.Message);
                Console.WriteLine(ex.InnerException.Source);
                Console.WriteLine(ex.InnerException.StackTrace);
                Console.WriteLine(ex.GeneratedSourceCode);
                var config = ex.RequestConfigurationData as RazorFolderHostTemplateConfiguration;
                if (config != null)
                {
                    Console.WriteLine(config.PhysicalPath);
                    Console.WriteLine(config.TemplatePath);
                    Console.WriteLine(config.TemplateRelativePath);
                    Console.WriteLine(config.LayoutPage);
                }


                exception = true;
            }

            Assert.IsTrue(exception, "Exception should have been thrown.");

            host.Stop();
        }
Example #3
0
        public static void StartRazorHost(Type[] modelTypes)
        {
            lock (_lock)
            {
                if (RazorHost != null)
                {
                    return;
                }

                var host = new RazorFolderHostContainer()
                {
                    // *** Set your Folder Path here - physical or relative ***
                    TemplatePath = Path.GetFullPath(@".\templates\"),
                    // *** Path to the Assembly path of your application
                    BaseBinaryFolder = Environment.CurrentDirectory
                };

                // Add any unique assemblies that are referenced in your templates
                var typesToAdd = modelTypes.GroupBy(x => x.Assembly).Select(x => x.First());
                foreach (var type in typesToAdd)
                {
                    host.AddAssemblyFromType(type);
                }

                // Always must start the host
                host.Start();

                RazorHost = host;
                IsStarted = true;
            }
        }
Example #4
0
 public static void StopRazorHost()
 {
     lock (_lock)
     {
         RazorHost?.Stop();
         RazorHost = null;
         IsStarted = false;
     }
 }
Example #5
0
        public void FolderWithPartialTest()
        {
            var host = new RazorFolderHostContainer();

            host.TemplatePath     = Path.GetFullPath(@"..\..\FileTemplates\");
            host.BaseBinaryFolder = Environment.CurrentDirectory;

            // add model assembly - ie. this assembly
            host.AddAssemblyFromType(typeof(Person));

            host.UseAppDomain = true;
            //host.Configuration.CompileToMemory = true;
            //host.Configuration.TempAssemblyPath = Environment.CurrentDirectory;

            host.Start();

            Person person = new Person()
            {
                Name    = "John Doe",
                Company = "Doeboy Incorporated",
                Entered = DateTime.Now,
                Address = new Address()
                {
                    Street = "32 Kaiea",
                    City   = "Paia"
                }
            };

            string result = host.RenderTemplate("~/TestPartial.cshtml", person);

            Console.WriteLine(result);
            Console.WriteLine("---");
            Console.WriteLine(host.Engine.LastGeneratedCode);

            if (result == null)
            {
                Assert.Fail(host.ErrorMessage);
            }

            // run again
            person.Name = "Billy Bobb";
            result      = host.RenderTemplate("~/TestPartial.cshtml", person);

            Console.WriteLine(result);
            Console.WriteLine("---");
            Console.WriteLine(host.Engine.LastGeneratedCode);

            if (result == null)
            {
                Assert.Fail(host.ErrorMessage);
            }


            Assert.IsTrue(result.Contains("Billy Bobb"));

            host.Stop();
        }
Example #6
0
        public void StopRazorHost()
        {
            if (RazorHost == null)
            {
                return;
            }

            RazorHost.Stop();
            RazorHost.Dispose();
            RazorHost = null;
        }
        private static RazorFolderHostContainer<RazorTemplateFolderHost> CreateHostContainer()
        {
            var host = new RazorFolderHostContainer<RazorTemplateFolderHost>();
            host.UseAppDomain = false;
            host.TemplatePath = App.UserDataPath + "Html\\";

            // add this assembly
            host.AddAssemblyFromType(typeof(ResultsParser));
            host.AddAssemblyFromType(typeof(AppConfiguration));

            host.Start();

            return host;
        }
        public void RuntimeErrorTest()
        {
            var host = new RazorFolderHostContainer();

            host.TemplatePath     = Path.GetFullPath(@"..\..\FileTemplates\");
            host.BaseBinaryFolder = Environment.CurrentDirectory;

            // add model assembly - ie. this assembly
            host.AddAssemblyFromType(typeof(Person));
            host.UseAppDomain = false;

            // these are implicitly set
            host.Configuration.CompileToMemory = false;
            //host.Configuration.TempAssemblyPath = Environment.CurrentDirectory;

            host.Start();

            Person person = new Person()
            {
                Name    = "Rick",
                Company = "West Wind",
                Entered = DateTime.Now,
                Address = new Address()
                {
                    Street = "32 Kaiea",
                    City   = "Paia"
                }
            };
            string result = host.RenderTemplate("~/RuntimeError.cshtml", person);

            Assert.IsNull(result, "result should be null on error");
            Console.WriteLine(host.ErrorMessage);  // simple message

            Assert.IsNotNull(host.LastException, "Last exception should be set");

            Console.WriteLine("---");
            Console.WriteLine(host.LastException.Message);
            Console.WriteLine(host.LastException.ActiveTemplate);
            //Console.WriteLine(host.LastException.GeneratedSourceCode);
            Console.WriteLine("---");

            // Render HTML output of the error with source code and line numbers for errors
            Console.WriteLine(host.RenderHtmlErrorPage());

            host.Stop();

            Assert.IsNull(result);
            Assert.IsTrue(!string.IsNullOrEmpty(host.ErrorMessage));
        }
Example #9
0
        public void BasicFolderWithRoslynCompilerTest()
        {
            var host = new RazorFolderHostContainer();

            host.CodeProvider = new Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider();

            host.TemplatePath     = Path.GetFullPath(@"..\..\FileTemplates\");
            host.BaseBinaryFolder = Environment.CurrentDirectory;

            // add model assembly - ie. this assembly
            host.AddAssemblyFromType(typeof(Person));
            host.UseAppDomain = false;

            // these are implicitly set
            //host.Configuration.CompileToMemory = true;
            //host.Configuration.TempAssemblyPath = Environment.CurrentDirectory;

            host.Start();

            Person person = new Person()
            {
                Name    = "Rick",
                Company = "West Wind",
                Entered = DateTime.Now,
                Address = new Address()
                {
                    Street = "32 Kaiea",
                    City   = "Paia"
                }
            };

            string result = host.RenderTemplate("~/HelloWorldCSharpLatest.cshtml", person);



            Console.WriteLine(result);
            Console.WriteLine("---");
            Console.WriteLine(host.ErrorMessage);
            Console.WriteLine(host.Engine.LastGeneratedCode);

            host.Stop();

            if (result == null)
            {
                Assert.Fail(host.ErrorMessage);
            }

            Assert.IsTrue(result.Contains("West Wind"));
        }
Example #10
0
        private RazorFolderHostContainer CreateRazorFolderHostContainer(Type type, [In] string str)
        {
            var folderHostContainer = new RazorFolderHostContainer
            {
                TemplatePath     = str,
                BaseBinaryFolder = Environment.CurrentDirectory,
                UseAppDomain     = false,
                Configuration    = { CompileToMemory = true }
            };

            folderHostContainer.AddAssemblyFromType(type);
            folderHostContainer.Start();

            return(folderHostContainer);
        }
Example #11
0
        private static RazorFolderHostContainer <RazorTemplateFolderHost> CreateHostContainer()
        {
            var host = new RazorFolderHostContainer <RazorTemplateFolderHost>();

            host.UseAppDomain = false;
            host.TemplatePath = App.UserDataPath + "Html\\";

            // add this assembly
            host.AddAssemblyFromType(typeof(ResultsParser));
            host.AddAssemblyFromType(typeof(AppConfiguration));

            host.Start();

            return(host);
        }
Example #12
0
        public void StringTemplateTest()
        {
            var host = new RazorFolderHostContainer();

            host.TemplatePath     = Path.GetFullPath(@"..\..\FileTemplates\");
            host.BaseBinaryFolder = Environment.CurrentDirectory;

            // add model assembly - ie. this assembly
            host.AddAssemblyFromType(typeof(Person));

            host.UseAppDomain = true;
            //host.Configuration.CompileToMemory = false;
            //host.Configuration.TempAssemblyPath = Environment.CurrentDirectory;

            host.Start();

            Person person = new Person()
            {
                Name    = "Rick",
                Company = "West Wind",
                Entered = DateTime.Now,
                Address = new Address()
                {
                    Street = "32 Kaiea",
                    City   = "Paia"
                }
            };

            string result = null;

            for (int i = 0; i < 10; i++)
            {
                result = host.RenderTemplate("~/TestRenderTemplate.cshtml", person);
            }

            Console.WriteLine(result);
            Console.WriteLine("---");
            Console.WriteLine(host.Engine.LastGeneratedCode);

            host.Stop();

            if (result == null)
            {
                Assert.Fail(host.ErrorMessage);
            }

            Assert.IsTrue(result.Contains("West Wind"));
        }
Example #13
0
        public string RenderViewToString <T>(T model, string templatePath, string templateName)
        {
            var host = new RazorFolderHostContainer()
            {
                TemplatePath     = templatePath,
                BaseBinaryFolder = Environment.CurrentDirectory
            };

            host.TemplatePath     = templatePath;
            host.BaseBinaryFolder = Environment.CurrentDirectory;
            host.AddAssemblyFromType(typeof(T));
            host.UseAppDomain = true;
            host.Start();
            string result = host.RenderTemplate("~/" + templateName, model);

            host.Stop();
            return(result);
        }
        public void BasicFolderTest()
        {
            var host = new RazorFolderHostContainer();

            host.TemplatePath = Path.GetFullPath(@"..\..\FileTemplates\");
            host.BaseBinaryFolder = Environment.CurrentDirectory;

            // add model assembly - ie. this assembly
            host.AddAssemblyFromType(typeof(Person));
            host.UseAppDomain = true;

            //host.Configuration.CompileToMemory = true;
            //host.Configuration.TempAssemblyPath = Environment.CurrentDirectory;

            host.Start();

            Person person = new Person()
            {
                Name = "Rick",
                Company = "West Wind",
                Entered = DateTime.Now,
                Address = new Address()
                {
                    Street = "32 Kaiea",
                    City = "Paia"
                }
            };

            string result = host.RenderTemplate("~/HelloWorld.cshtml", person);



            Console.WriteLine(result);
            Console.WriteLine("---");
            Console.WriteLine(host.Engine.LastGeneratedCode);

            host.Stop();

            if (result == null)
                Assert.Fail(host.ErrorMessage);

            Assert.IsTrue(result.Contains("West Wind"));
        }
Example #15
0
        public void RuntimeErrorTest()
        {
            var host = new RazorFolderHostContainer();

            host.TemplatePath     = Path.GetFullPath(@"..\..\FileTemplates\");
            host.BaseBinaryFolder = Environment.CurrentDirectory;

            // add model assembly - ie. this assembly
            host.AddAssemblyFromType(typeof(Person));
            host.UseAppDomain = false;

            // these are implicitly set
            host.Configuration.CompileToMemory = false;
            //host.Configuration.TempAssemblyPath = Environment.CurrentDirectory;

            host.Start();

            Person person = new Person()
            {
                Name    = "Rick",
                Company = "West Wind",
                Entered = DateTime.Now,
                Address = new Address()
                {
                    Street = "32 Kaiea",
                    City   = "Paia"
                }
            };

            string result = host.RenderTemplate("~/RuntimeError.cshtml", person);

            Console.WriteLine(result);
            Console.WriteLine("---");
            Console.WriteLine(host.ErrorMessage);
            Console.WriteLine("---");
            Console.WriteLine(host.Engine.LastGeneratedCode);

            host.Stop();

            Assert.IsNull(result);
            Assert.IsTrue(!string.IsNullOrEmpty(host.ErrorMessage));
        }
        public void MissingTemplateTest()
        {
            var host = new RazorFolderHostContainer();

            host.TemplatePath     = Path.GetFullPath(@"..\..\FileTemplates\");
            host.BaseBinaryFolder = Environment.CurrentDirectory;

            // add model assembly - ie. this assembly
            host.AddAssemblyFromType(typeof(Person));

            host.UseAppDomain = true;
            //host.Configuration.CompileToMemory = true;
            //host.Configuration.TempAssemblyPath = Environment.CurrentDirectory;

            host.Start();

            Person person = new Person()
            {
                Name    = "Rick",
                Company = "West Wind",
                Entered = DateTime.Now,
                Address = new Address()
                {
                    Street = "32 Kaiea",
                    City   = "Paia"
                }
            };

            string result = host.RenderTemplate("~/NotThere.cshtml", person);

            Assert.IsTrue(host.ErrorMessage.Contains("Template File doesn't exist"));
            Assert.IsNotNull(host.LastException);


            Console.WriteLine(host.ErrorMessage);

            Console.WriteLine("template: " + host.LastException.ActiveTemplate);


            host.Stop();
        }
Example #17
0
 public HostingContext(string relativePath)
 {
     _host = new RazorFolderHostContainer {
         TemplatePath = Path.Combine(Environment.CurrentDirectory, relativePath)
     };
 }
Example #18
0
        public void FolderHostWithLayoutPageTest()
        {
            using (var host = new RazorFolderHostContainer())
            {
                host.TemplatePath     = Path.GetFullPath(@"..\..\FileTemplates\");
                host.BaseBinaryFolder = Environment.CurrentDirectory;
                Console.WriteLine(host.TemplatePath);

                // point at the folder where dependent assemblies can be found
                // this applies only to separate AppDomain hosting
                host.BaseBinaryFolder = Environment.CurrentDirectory;

                // add model assembly - ie. this assembly
                host.AddAssemblyFromType(typeof(Person));

                // NOTE: If you use AppDomains you will need to add a /bin folder
                //       with all dependencies OR run out of the current folder
                //       and all models have to be serializable or MarshalByRefObj
                host.UseAppDomain = false;

                //host.Configuration.CompileToMemory = true;
                //host.Configuration.TempAssemblyPath = Environment.CurrentDirectory;

                // Always must start the host
                host.Start();

                // create a model to pass
                Person person = new Person()
                {
                    Name    = "John Doe",
                    Company = "Doeboy Incorporated",
                    Entered = DateTime.Now,
                    Address = new Address()
                    {
                        Street = "32 Kaiea",
                        City   = "Paia"
                    }
                };

                Console.WriteLine("-----Layout page only (rendered)");
                // just show what a layout template looks like on its own
                string layout = host.RenderTemplate("~/_Layout.cshtml", person);
                Console.WriteLine(layout);


                Console.WriteLine("----- Content page In Layout Container");

                // render a template and pass the model
                string result = host.RenderTemplate("~/LayoutPageExample.cshtml", person);

                //result = layout.Replace("@RenderBody", result);

                //Assert.True(result != null, "Template didn't return any data: " + host.ErrorMessage);

                Console.WriteLine("---");
                Console.WriteLine(result);
                Console.WriteLine("---");

                Assert.IsNotNull(result, host.ErrorMessage);
            }
        }