当前位置: 首页>>代码示例>>C#>>正文


C# RazorView.RenderAsync方法代码示例

本文整理汇总了C#中Microsoft.AspNet.Mvc.Razor.RazorView.RenderAsync方法的典型用法代码示例。如果您正苦于以下问题:C# RazorView.RenderAsync方法的具体用法?C# RazorView.RenderAsync怎么用?C# RazorView.RenderAsync使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Microsoft.AspNet.Mvc.Razor.RazorView的用法示例。


在下文中一共展示了RazorView.RenderAsync方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: RenderAsync_AsPartial_ActivatesViews_WithThePassedInViewContext

        public async Task RenderAsync_AsPartial_ActivatesViews_WithThePassedInViewContext()
        {
            // Arrange
            var viewData = new ViewDataDictionary(new EmptyModelMetadataProvider());
            var page = new TestableRazorPage(v =>
            {
                // viewData is assigned to ViewContext by the activator
                Assert.Same(viewData, v.ViewContext.ViewData);
            });
            var activator = new Mock<IRazorPageActivator>();
            var view = new RazorView(Mock.Of<IRazorViewEngine>(),
                                     activator.Object,
                                     CreateViewStartProvider(),
                                     page,
                                     isPartial: true);

            var viewContext = CreateViewContext(view);
            var expectedWriter = viewContext.Writer;
            activator.Setup(a => a.Activate(page, It.IsAny<ViewContext>()))
                     .Callback((IRazorPage p, ViewContext c) =>
                     {
                         Assert.Same(c, viewContext);
                         c.ViewData = viewData;
                     })
                     .Verifiable();

            // Act
            await view.RenderAsync(viewContext);

            // Assert
            activator.Verify();
            Assert.Same(expectedWriter, viewContext.Writer);
        }
开发者ID:RehanSaeed,项目名称:Mvc,代码行数:33,代码来源:RazorViewTest.cs

示例2: RenderAsync_AsPartial_BuffersOutput

        public async Task RenderAsync_AsPartial_BuffersOutput()
        {
            // Arrange
            TextWriter actual = null;
            var page = new TestableRazorPage(v =>
            {
                actual = v.Output;
                v.HtmlEncoder = new CommonTestEncoder();
                v.Write("Hello world");
            });
            var view = new RazorView(Mock.Of<IRazorViewEngine>(),
                                     Mock.Of<IRazorPageActivator>(),
                                     CreateViewStartProvider(),
                                     page,
                                     isPartial: true);
            var viewContext = CreateViewContext(view);
            var expected = viewContext.Writer;

            // Act
            await view.RenderAsync(viewContext);

            // Assert
            Assert.NotSame(expected, actual);
            Assert.IsAssignableFrom<IBufferedTextWriter>(actual);
            Assert.Equal("HtmlEncode[[Hello world]]", viewContext.Writer.ToString());
        }
开发者ID:RehanSaeed,项目名称:Mvc,代码行数:26,代码来源:RazorViewTest.cs

示例3: RenderAsync_WithoutHierarchy_DoesNotCreateOutputBuffer

        public async Task RenderAsync_WithoutHierarchy_DoesNotCreateOutputBuffer()
        {
            // Arrange
            TextWriter actual = null;
            var page = new TestableRazorPage(v =>
            {
                actual = v.Output;
                v.Write("Hello world");
            });
            var view = new RazorView(Mock.Of<IRazorPageFactory>(),
                                     Mock.Of<IRazorPageActivator>(),
                                     CreateViewStartProvider(),
                                     page,
                                     executeViewHierarchy: false);
            var viewContext = CreateViewContext(view);
            var expected = viewContext.Writer;

            // Act
            await view.RenderAsync(viewContext);

            // Assert
            Assert.Same(expected, actual);
            Assert.Equal("Hello world", expected.ToString());
        }
开发者ID:Nakro,项目名称:Mvc,代码行数:24,代码来源:RazorViewTest.cs

示例4: RenderAsync_ExecutesViewStart

        public async Task RenderAsync_ExecutesViewStart()
        {
            // Arrange
            var actualLayoutPath = "";
            var layoutPath = "/Views/_Shared/_Layout.cshtml";
            var viewStart1 = new TestableRazorPage(v =>
            {
                v.Layout = "/fake-layout-path";
            });
            var viewStart2 = new TestableRazorPage(v =>
            {
                v.Layout = layoutPath;
            });
            var page = new TestableRazorPage(v =>
            {
                // This path must have been set as a consequence of running viewStart
                actualLayoutPath = v.Layout;
                // Clear out layout so we don't render it
                v.Layout = null;
            });
            var activator = new Mock<IRazorPageActivator>();
            activator.Setup(a => a.Activate(viewStart1, It.IsAny<ViewContext>()))
                     .Verifiable();
            activator.Setup(a => a.Activate(viewStart2, It.IsAny<ViewContext>()))
                     .Verifiable();
            activator.Setup(a => a.Activate(page, It.IsAny<ViewContext>()))
                     .Verifiable();
            var view = new RazorView(Mock.Of<IRazorViewEngine>(),
                                     activator.Object,
                                     CreateViewStartProvider(viewStart1, viewStart2),
                                     page,
                                     isPartial: false);
            var viewContext = CreateViewContext(view);

            // Act
            await view.RenderAsync(viewContext);

            // Assert
            activator.Verify();
        }
开发者ID:RehanSaeed,项目名称:Mvc,代码行数:40,代码来源:RazorViewTest.cs

示例5: RenderAsync_ThrowsIfLayoutPageCannotBeFound

        public async Task RenderAsync_ThrowsIfLayoutPageCannotBeFound()
        {
            // Arrange
            var expected = string.Join(Environment.NewLine,
                                       "The layout view 'Does-Not-Exist-Layout' could not be located. " +
                                       "The following locations were searched:",
                                       "path1",
                                       "path2");

            var layoutPath = "Does-Not-Exist-Layout";
            var page = new TestableRazorPage(v =>
            {
                v.Layout = layoutPath;
            });

            var viewEngine = new Mock<IRazorViewEngine>();
            var activator = new Mock<IRazorPageActivator>();
            var view = new RazorView(viewEngine.Object,
                                     Mock.Of<IRazorPageActivator>(),
                                     Mock.Of<IViewStartProvider>(),
                                     page,
                                     isPartial: false);
            var viewContext = CreateViewContext(view);
            viewEngine.Setup(v => v.FindPage(viewContext, layoutPath))
                      .Returns(new RazorPageResult(layoutPath, new[] { "path1", "path2" }))
                      .Verifiable();

            // Act
            var ex = await Assert.ThrowsAsync<InvalidOperationException>(() => view.RenderAsync(viewContext));

            // Assert
            Assert.Equal(expected, ex.Message);
            viewEngine.Verify();
        }
开发者ID:RehanSaeed,项目名称:Mvc,代码行数:34,代码来源:RazorViewTest.cs

示例6: ViewContext_ExecutingPagePath_ReturnsPathOfRazorPageBeingExecuted

        public async Task ViewContext_ExecutingPagePath_ReturnsPathOfRazorPageBeingExecuted()
        {
            // Arrange
            var pagePath = "/my/view";
            var paths = new List<string>();
            var page = new TestableRazorPage(v =>
            {
                paths.Add(v.ViewContext.ExecutingFilePath);
                Assert.Equal(pagePath, v.ViewContext.View.Path);
            })
            {
                Path = pagePath
            };

            var viewStart = new TestableRazorPage(v =>
            {
                v.Layout = LayoutPath;
                paths.Add(v.ViewContext.ExecutingFilePath);
                Assert.Equal(pagePath, v.ViewContext.View.Path);
            })
            {
                Path = "_ViewStart"
            };

            var layout = new TestableRazorPage(v =>
            {
                v.RenderBodyPublic();
                paths.Add(v.ViewContext.ExecutingFilePath);
                Assert.Equal(pagePath, v.ViewContext.View.Path);
            })
            {
                Path = LayoutPath
            };

            var activator = Mock.Of<IRazorPageActivator>();
            var viewEngine = new Mock<IRazorViewEngine>();
            viewEngine.Setup(v => v.FindPage(It.IsAny<ActionContext>(), LayoutPath))
                      .Returns(new RazorPageResult(LayoutPath, layout));
            var view = new RazorView(viewEngine.Object,
                                     activator,
                                     CreateViewStartProvider(viewStart),
                                     page,
                                     isPartial: false);

            var viewContext = CreateViewContext(view);
            var expectedWriter = viewContext.Writer;

            // Act
            await view.RenderAsync(viewContext);

            // Assert
            Assert.Equal(new[] { "_ViewStart", pagePath, LayoutPath }, paths);
        }
开发者ID:RehanSaeed,项目名称:Mvc,代码行数:53,代码来源:RazorViewTest.cs

示例7: RenderAsync_UsesPageExecutionFeatureFromRequest_ToWrapWriter

        public async Task RenderAsync_UsesPageExecutionFeatureFromRequest_ToWrapWriter()
        {
            // Arrange
            var pageWriter = CreateBufferedWriter();
            var layoutWriter = CreateBufferedWriter();

            var layoutExecuted = false;
            var count = -1;
            var feature = new Mock<IPageExecutionListenerFeature>(MockBehavior.Strict);
            feature.Setup(f => f.DecorateWriter(It.IsAny<RazorTextWriter>()))
                   .Returns(() =>
                   {
                       count++;
                       if (count == 0)
                       {
                           return pageWriter;
                       }
                       else if (count == 1)
                       {
                           return layoutWriter;
                       }
                       throw new Exception();
                   })
                   .Verifiable();

            var pageContext = Mock.Of<IPageExecutionContext>();
            feature.Setup(f => f.GetContext("/MyPage.cshtml", pageWriter))
                    .Returns(pageContext)
                    .Verifiable();

            var layoutContext = Mock.Of<IPageExecutionContext>();
            feature.Setup(f => f.GetContext("/Layout.cshtml", layoutWriter))
                    .Returns(layoutContext)
                    .Verifiable();

            var page = new TestableRazorPage(v =>
            {
                v.HtmlEncoder = new CommonTestEncoder();
                v.Layout = "Layout";
                Assert.Same(pageWriter, v.Output);
                Assert.Same(pageContext, v.PageExecutionContext);
            });
            page.Path = "/MyPage.cshtml";

            var layout = new TestableRazorPage(v =>
            {
                v.HtmlEncoder = new CommonTestEncoder();
                Assert.Same(layoutWriter, v.Output);
                Assert.Same(layoutContext, v.PageExecutionContext);
                v.RenderBodyPublic();

                layoutExecuted = true;
            });
            layout.Path = "/Layout.cshtml";

            var viewEngine = new Mock<IRazorViewEngine>();
            viewEngine.Setup(p => p.FindPage(It.IsAny<ActionContext>(), "Layout"))
                       .Returns(new RazorPageResult("Layout", layout));
            var viewStartProvider = new Mock<IViewStartProvider>();
            viewStartProvider.Setup(v => v.GetViewStartPages(It.IsAny<string>()))
                             .Returns(Enumerable.Empty<IRazorPage>())
                             .Verifiable();
            var view = new RazorView(viewEngine.Object,
                                     Mock.Of<IRazorPageActivator>(),
                                     viewStartProvider.Object,
                                     page,
                                     isPartial: false);
            var viewContext = CreateViewContext(view);
            viewContext.HttpContext.SetFeature<IPageExecutionListenerFeature>(feature.Object);

            // Act
            await view.RenderAsync(viewContext);

            // Assert
            feature.Verify();
            viewStartProvider.Verify();
            Assert.True(layoutExecuted);
        }
开发者ID:RehanSaeed,项目名称:Mvc,代码行数:78,代码来源:RazorViewTest.cs

示例8: RenderAsync_ExecutesNestedLayoutPages

        public async Task RenderAsync_ExecutesNestedLayoutPages()
        {
            // Arrange
            var htmlEncoder = new CommonTestEncoder();
            var expected = "HtmlEncode[[layout-2" +
                           Environment.NewLine +
                           "]]bar-content" +
                           Environment.NewLine +
                           "HtmlEncode[[layout-1" +
                           Environment.NewLine +
                           "]]foo-content" +
                           Environment.NewLine +
                           "body-content";

            var page = new TestableRazorPage(v =>
            {
                v.HtmlEncoder = htmlEncoder;
                v.DefineSection("foo", async writer =>
                {
                    await writer.WriteLineAsync("foo-content");
                });
                v.Layout = "~/Shared/Layout1.cshtml";
                v.WriteLiteral("body-content");
            });
            var layout1 = new TestableRazorPage(v =>
            {
                v.HtmlEncoder = htmlEncoder;
                v.Write("layout-1" + Environment.NewLine);
                v.Write(v.RenderSection("foo"));
                v.DefineSection("bar", writer => writer.WriteLineAsync("bar-content"));
                v.RenderBodyPublic();
                v.Layout = "~/Shared/Layout2.cshtml";
            });
            layout1.Path = "~/Shared/Layout1.cshtml";

            var layout2 = new TestableRazorPage(v =>
            {
                v.HtmlEncoder = htmlEncoder;
                v.Write("layout-2" + Environment.NewLine);
                v.Write(v.RenderSection("bar"));
                v.RenderBodyPublic();
            });
            layout2.Path = "~/Shared/Layout2.cshtml";

            var viewEngine = new Mock<IRazorViewEngine>();
            viewEngine.Setup(p => p.FindPage(It.IsAny<ActionContext>(), "~/Shared/Layout1.cshtml"))
                       .Returns(new RazorPageResult("~/Shared/Layout1.cshtml", layout1));
            viewEngine.Setup(p => p.FindPage(It.IsAny<ActionContext>(), "~/Shared/Layout2.cshtml"))
                       .Returns(new RazorPageResult("~/Shared/Layout2.cshtml", layout2));

            var view = new RazorView(viewEngine.Object,
                                     Mock.Of<IRazorPageActivator>(),
                                     CreateViewStartProvider(),
                                     page,
                                     new CommonTestEncoder(),
                                     isPartial: false);
            var viewContext = CreateViewContext(view);

            // Act
            await view.RenderAsync(viewContext);

            // Assert
            Assert.Equal(expected, viewContext.Writer.ToString());
        }
开发者ID:4myBenefits,项目名称:Mvc,代码行数:64,代码来源:RazorViewTest.cs

示例9: RenderAsync_ThrowsIfBodyWasNotRendered

        public async Task RenderAsync_ThrowsIfBodyWasNotRendered()
        {
            // Arrange
            var page = new TestableRazorPage(v =>
            {
                v.Layout = LayoutPath;
            });
            var layout = new TestableRazorPage(v =>
            {
            })
            {
                Path = LayoutPath
            };
            var viewEngine = new Mock<IRazorViewEngine>();
            viewEngine.Setup(p => p.FindPage(It.IsAny<ActionContext>(), LayoutPath))
                       .Returns(new RazorPageResult(LayoutPath, layout));

            var view = new RazorView(viewEngine.Object,
                                     Mock.Of<IRazorPageActivator>(),
                                     CreateViewStartProvider(),
                                     page,
                                     isPartial: false);
            var viewContext = CreateViewContext(view);

            // Act and Assert
            var ex = await Assert.ThrowsAsync<InvalidOperationException>(() => view.RenderAsync(viewContext));
            Assert.Equal($"RenderBody has not been called for the page at '{LayoutPath}'.", ex.Message);
        }
开发者ID:RehanSaeed,项目名称:Mvc,代码行数:28,代码来源:RazorViewTest.cs

示例10: IsPartial_IsSetToTrue_ForPartialView

        public async Task IsPartial_IsSetToTrue_ForPartialView()
        {
            // Arrange
            bool? isPartialPage = null;
            var page = new TestableRazorPage(v =>
            {
                isPartialPage = v.IsPartial;
            });
            var view = new RazorView(Mock.Of<IRazorViewEngine>(),
                                     Mock.Of<IRazorPageActivator>(),
                                     CreateViewStartProvider(),
                                     page,
                                     isPartial: true);
            var viewContext = CreateViewContext(view);

            // Act
            await view.RenderAsync(viewContext);

            // Assert
            Assert.True(isPartialPage.Value);
        }
开发者ID:RehanSaeed,项目名称:Mvc,代码行数:21,代码来源:RazorViewTest.cs

示例11: RenderAsync_SucceedsIfNestedSectionsAreRendered

        public async Task RenderAsync_SucceedsIfNestedSectionsAreRendered()
        {
            // Arrange
            var expected = string.Join(
                Environment.NewLine,
                "layout-section-content",
                "page-section-content");

            var htmlEncoder = new CommonTestEncoder();
            var page = new TestableRazorPage(v =>
            {
                v.HtmlEncoder = htmlEncoder;
                v.Layout = "~/Shared/Layout1.cshtml";
                v.DefineSection("foo", async writer =>
                {
                    await writer.WriteAsync("page-section-content");
                });
            });
            var nestedLayout = new TestableRazorPage(v =>
            {
                v.HtmlEncoder = htmlEncoder;
                v.Layout = "~/Shared/Layout2.cshtml";
                v.RenderBodyPublic();
                v.DefineSection("foo", async writer =>
                {
                    await writer.WriteLineAsync("layout-section-content");
                    await v.RenderSectionAsync("foo");
                });
            })
            {
                Path = "/Shared/Layout1.cshtml"
            };
            var baseLayout = new TestableRazorPage(v =>
            {
                v.HtmlEncoder = htmlEncoder;
                v.RenderBodyPublic();
                v.RenderSection("foo");
            })
            {
                Path = "/Shared/Layout2.cshtml"
            };

            var viewEngine = new Mock<IRazorViewEngine>();
            viewEngine.Setup(p => p.FindPage(It.IsAny<ActionContext>(), "~/Shared/Layout1.cshtml"))
                       .Returns(new RazorPageResult("~/Shared/Layout1.cshtml", nestedLayout));
            viewEngine.Setup(p => p.FindPage(It.IsAny<ActionContext>(), "~/Shared/Layout2.cshtml"))
                       .Returns(new RazorPageResult("~/Shared/Layout2.cshtml", baseLayout));

            var view = new RazorView(viewEngine.Object,
                                     Mock.Of<IRazorPageActivator>(),
                                     CreateViewStartProvider(),
                                     page,
                                     isPartial: false);
            var viewContext = CreateViewContext(view);

            // Act
            await view.RenderAsync(viewContext);

            // Assert
            Assert.Equal(expected, viewContext.Writer.ToString());
        }
开发者ID:RehanSaeed,项目名称:Mvc,代码行数:61,代码来源:RazorViewTest.cs

示例12: ResettingLayout_InViewStartCausesItToBeResetInPage

        public async Task ResettingLayout_InViewStartCausesItToBeResetInPage()
        {
            // Arrange
            var expected = "Layout";
            string actual = null;

            var page = new TestableRazorPage(v =>
            {
                Assert.Null(v.Layout);
            });
            var viewStart1 = new TestableRazorPage(v =>
            {
                v.Layout = expected;
            });
            var viewStart2 = new TestableRazorPage(v =>
            {
                actual = v.Layout;
                v.Layout = null;
            });
            var viewEngine = Mock.Of<IRazorViewEngine>();

            var view = new RazorView(viewEngine,
                                     Mock.Of<IRazorPageActivator>(),
                                     CreateViewStartProvider(viewStart1, viewStart2),
                                     page,
                                     isPartial: false);
            var viewContext = CreateViewContext(view);

            // Act
            await view.RenderAsync(viewContext);

            // Assert
            Assert.Equal(expected, actual);
        }
开发者ID:RehanSaeed,项目名称:Mvc,代码行数:34,代码来源:RazorViewTest.cs

示例13: IsPartial_IsSetToFalse_ForViewStartPageAndLayoutOfAView

        public async Task IsPartial_IsSetToFalse_ForViewStartPageAndLayoutOfAView()
        {
            // Arrange
            bool? isPartialPage = null;
            bool? isPartialLayout = null;
            bool? isPartialViewStart = null;

            var page = new TestableRazorPage(v =>
            {
                isPartialPage = v.IsPartial;
            });
            var viewStart = new TestableRazorPage(v =>
            {
                v.Layout = "/Layout.cshtml";
                isPartialViewStart = v.IsPartial;
            });
            var layout = new TestableRazorPage(v =>
            {
                isPartialLayout = v.IsPartial;
                v.RenderBodyPublic();
            });
            var viewEngine = new Mock<IRazorViewEngine>();
            viewEngine.Setup(p => p.FindPage(It.IsAny<ActionContext>(), "/Layout.cshtml"))
                       .Returns(new RazorPageResult("Layout", layout));

            var view = new RazorView(viewEngine.Object,
                                     Mock.Of<IRazorPageActivator>(),
                                     CreateViewStartProvider(viewStart),
                                     page,
                                     isPartial: false);
            var viewContext = CreateViewContext(view);

            // Act
            await view.RenderAsync(viewContext);

            // Assert
            Assert.False(isPartialPage.Value);
            Assert.False(isPartialLayout.Value);
            Assert.False(isPartialViewStart.Value);
        }
开发者ID:RehanSaeed,项目名称:Mvc,代码行数:40,代码来源:RazorViewTest.cs

示例14: RenderAsync_CopiesLayoutPropertyFromViewStart

        public async Task RenderAsync_CopiesLayoutPropertyFromViewStart()
        {
            // Arrange
            var expectedViewStart = "Layout1";
            var expectedPage = "Layout2";
            string actualViewStart = null;
            string actualPage = null;
            var page = new TestableRazorPage(v =>
            {
                actualPage = v.Layout;
                // Clear it out because we don't care about rendering the layout in this test.
                v.Layout = null;
            });
            var viewStart1 = new TestableRazorPage(v =>
            {
                v.Layout = expectedViewStart;
            });
            var viewStart2 = new TestableRazorPage(v =>
            {
                actualViewStart = v.Layout;
                v.Layout = expectedPage;
            });
            var viewEngine = Mock.Of<IRazorViewEngine>();

            var view = new RazorView(viewEngine,
                                     Mock.Of<IRazorPageActivator>(),
                                     CreateViewStartProvider(viewStart1, viewStart2),
                                     page,
                                     isPartial: false);
            var viewContext = CreateViewContext(view);

            // Act
            await view.RenderAsync(viewContext);

            // Assert
            Assert.Equal(expectedViewStart, actualViewStart);
            Assert.Equal(expectedPage, actualPage);
        }
开发者ID:RehanSaeed,项目名称:Mvc,代码行数:38,代码来源:RazorViewTest.cs

示例15: RenderAsync_DoesNotSetExecutionContextWhenListenerIsNotRegistered

        public async Task RenderAsync_DoesNotSetExecutionContextWhenListenerIsNotRegistered(bool isPartial)
        {
            // Arrange
            var executed = false;
            var page = new TestableRazorPage(v =>
            {
                Assert.Null(v.PageExecutionContext);
                executed = true;
            });

            var view = new RazorView(Mock.Of<IRazorViewEngine>(),
                                     Mock.Of<IRazorPageActivator>(),
                                     Mock.Of<IViewStartProvider>(),
                                     page,
                                     isPartial);
            var viewContext = CreateViewContext(view);

            // Act
            await view.RenderAsync(viewContext);

            // Assert
            Assert.True(executed);
        }
开发者ID:RehanSaeed,项目名称:Mvc,代码行数:23,代码来源:RazorViewTest.cs


注:本文中的Microsoft.AspNet.Mvc.Razor.RazorView.RenderAsync方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。