Example #1
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 #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 string RenderTemplate(string templateName, object model)
        {
            string result = hostContainer.RenderTemplate(templateName, model);

            if (result == null)
            {
                result = "<pre>" + hostContainer.ErrorMessage + "\r\n------\r\n" + hostContainer.Engine.LastGeneratedCode + "</pre>";
            }

            return(result);
        }
Example #4
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"));
        }
        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 #6
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 #7
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);
        }
Example #8
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 #10
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);
            }
        }
        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"));
        }