コード例 #1
0
        public void Issue163_SectionRedefinition()
        {
            RazorEngineServiceTestFixture.RunTestHelper(service =>
            {
                string parentLayoutTemplate = @"<script scr=""/Scripts/jquery.js""></script>@RenderSection(""Scripts"", false)";
                string childLayoutTemplate  =
                    @"@{ Layout = ""ParentLayout""; }@section Scripts {<script scr=""/Scripts/childlayout.js""></script>@RenderSection(""Scripts"", false)}";

                var parentKey = service.GetKey("ParentLayout");
                var childKey  = service.GetKey("ChildLayout");

                service.AddTemplate(parentKey, parentLayoutTemplate);
                service.AddTemplate(childKey, childLayoutTemplate);

                service.Compile(parentKey);
                service.Compile(childKey);

                // Page with no section defined (e.g. page has no own scripts)
                string pageWithoutOwnScriptsTemplate       = @"@{ Layout = ""ChildLayout""; }";
                string expectedPageWithoutOwnScriptsResult =
                    @"<script scr=""/Scripts/jquery.js""></script><script scr=""/Scripts/childlayout.js""></script>";

                using (var writer = new StringWriter())
                {
                    var pageKey = service.GetKey(nameof(pageWithoutOwnScriptsTemplate));

                    service.AddTemplate(pageKey, pageWithoutOwnScriptsTemplate);
                    string actualPageWithoutOwnScriptsResult = service.RunCompile(pageKey);

                    Assert.AreEqual(expectedPageWithoutOwnScriptsResult, actualPageWithoutOwnScriptsResult);
                }

                // Page with section redefenition (page has own additional scripts)
                string pageWithOwnScriptsTemplate       = @"@{ Layout = ""ChildLayout""; }@section Scripts {<script scr=""/Scripts/page.js""></script>}";
                string expectedPageWithOwnScriptsResult =
                    @"<script scr=""/Scripts/jquery.js""></script><script scr=""/Scripts/childlayout.js""></script><script scr=""/Scripts/page.js""></script>";

                using (var writer = new StringWriter())
                {
                    var pageKey2 = service.GetKey(nameof(pageWithOwnScriptsTemplate));

                    service.AddTemplate(pageKey2, pageWithOwnScriptsTemplate);
                    service.RunCompile(pageKey2, writer);

                    string actualPageWithOwnScriptsResult = writer.ToString();
                    Assert.AreEqual(expectedPageWithOwnScriptsResult, actualPageWithOwnScriptsResult);
                }
            });
        }
コード例 #2
0
        public void RazorEngineService_Issue267Ext()
        {
            var template = @"test";

            try
            {
                System.Runtime.Remoting.Messaging.CallContext.LogicalSetData("Unserializable", new Unserializable());
                RazorEngineServiceTestFixture.RunTestHelper(service =>
                {
                    service.Compile(template, "key", null);
                    string result = service.Run("key", null, (object)null);
                    Assert.AreEqual("test", result.Trim());
                });
            }
            finally
            {
                System.Runtime.Remoting.Messaging.CallContext.FreeNamedDataSlot("Unserializable");
            }
        }
コード例 #3
0
        public void Issue67_CollectionOfAnonymous()
        {
            RazorEngineServiceTestFixture.RunTestHelper(service =>
            {
                const string template = @"@foreach(var x in Model){
@x.Number
}";
                string expected       = "123";
                var list = new List <Person>()
                {
                    new Person()
                    {
                        Age = 1
                    },
                    new Person()
                    {
                        Age = 2
                    },
                    new Person()
                    {
                        Age = 3
                    }
                };
                dynamic collectionOfAnonymous = list.Select(p => new { Number = p.Age }).ToList();
                var arrayOfAnonymous          = collectionOfAnonymous.ToArray();
                string applied_1 = service.RunCompile(template, "test1", null, (object)collectionOfAnonymous);
                //string applied_2 = service.RunCompileOnDemand(template, "test2", (Type)collectionOfAnonymous.GetType(), (object)collectionOfAnonymous);
                string applied_3 = service.RunCompile(template, "test3", null, (object)arrayOfAnonymous);
                //string applied_4 = service.RunCompileOnDemand(template, "test4", (Type)arrayOfAnonymous.GetType(), (object)arrayOfAnonymous);

                Assert.AreEqual(expected, applied_1);
                //Assert.AreEqual(expected, applied_2);
                Assert.AreEqual(expected, applied_3);
                //Assert.AreEqual(expected, applied_4);
            });
        }