本文整理汇总了C#中Microsoft.AspNet.Mvc.Razor.RazorView类的典型用法代码示例。如果您正苦于以下问题:C# RazorView类的具体用法?C# RazorView怎么用?C# RazorView使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
RazorView类属于Microsoft.AspNet.Mvc.Razor命名空间,在下文中一共展示了RazorView类的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);
}
示例2: GetView
/// <inheritdoc />
public IView GetView([NotNull] IRazorViewEngine viewEngine,
[NotNull] IRazorPage page,
bool isPartial)
{
var razorView = new RazorView(viewEngine, _pageActivator, _viewStartProvider, page, isPartial);
return razorView;
}
示例3: 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());
}
示例4: 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());
}
示例5: GetView
/// <inheritdoc />
public IView GetView(
IRazorViewEngine viewEngine,
IRazorPage page,
bool isPartial)
{
if (viewEngine == null)
{
throw new ArgumentNullException(nameof(viewEngine));
}
if (page == null)
{
throw new ArgumentNullException(nameof(page));
}
var razorView = new RazorView(
viewEngine,
_pageActivator,
_viewStartProvider,
page,
_htmlEncoder,
isPartial);
return razorView;
}
示例6: RenderAsync_ThrowsIfSectionsWereDefinedButNotRendered
public async Task RenderAsync_ThrowsIfSectionsWereDefinedButNotRendered()
{
// Arrange
var page = new TestableRazorPage(v =>
{
v.DefineSection("head", _nullRenderAsyncDelegate);
v.Layout = LayoutPath;
v.DefineSection("foot", _nullRenderAsyncDelegate);
});
var layout = new TestableRazorPage(v =>
{
v.RenderBodyPublic();
})
{
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("The following sections have been defined but have not been rendered by the page "
+ $"at '{LayoutPath}': 'head, foot'.", ex.Message);
}
示例7: RenderAsync_ExecutesLayoutPages
public async Task RenderAsync_ExecutesLayoutPages()
{
// Arrange
var htmlEncoder = new CommonTestEncoder();
var htmlEncodedNewLine = htmlEncoder.HtmlEncode(Environment.NewLine);
var expected = "HtmlEncode[[layout-content" +
Environment.NewLine +
"]]head-content" +
htmlEncodedNewLine +
"body-content" +
htmlEncodedNewLine +
"foot-content";
var page = new TestableRazorPage(v =>
{
v.HtmlEncoder = htmlEncoder;
v.WriteLiteral("body-content");
v.Layout = LayoutPath;
v.DefineSection("head", async writer =>
{
await writer.WriteAsync("head-content");
});
v.DefineSection("foot", async writer =>
{
await writer.WriteAsync("foot-content");
});
});
var layout = new TestableRazorPage(v =>
{
v.HtmlEncoder = htmlEncoder;
v.Write("layout-content" + Environment.NewLine);
v.Write(v.RenderSection("head"));
v.Write(Environment.NewLine);
v.RenderBodyPublic();
v.Write(Environment.NewLine);
v.Write(v.RenderSection("foot"));
});
var activator = new Mock<IRazorPageActivator>();
activator.Setup(a => a.Activate(page, It.IsAny<ViewContext>()))
.Verifiable();
activator.Setup(a => a.Activate(layout, It.IsAny<ViewContext>()))
.Verifiable();
var viewEngine = new Mock<IRazorViewEngine>();
var view = new RazorView(viewEngine.Object,
activator.Object,
CreateViewStartProvider(),
page,
isPartial: false);
var viewContext = CreateViewContext(view);
viewEngine.Setup(p => p.FindPage(viewContext, LayoutPath))
.Returns(new RazorPageResult(LayoutPath, layout))
.Verifiable();
// Act
await view.RenderAsync(viewContext);
// Assert
// Verify the activator was invoked for the primary page and layout page.
activator.Verify();
Assert.Equal(expected, viewContext.Writer.ToString());
viewEngine.Verify();
}
示例8: 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();
}
示例9: 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);
}
示例10: 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);
}
示例11: 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);
}
示例12: 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);
}
示例13: RenderAsync_CreatesOutputBuffer
public async Task RenderAsync_CreatesOutputBuffer()
{
// Arrange
TextWriter actual = null;
var page = new TestableRazorPage(v =>
{
actual = v.Output;
});
var view = new RazorView(Mock.Of<IRazorViewEngine>(),
Mock.Of<IRazorPageActivator>(),
CreateViewStartProvider(),
page,
isPartial: false);
var viewContext = CreateViewContext(view);
var original = viewContext.Writer;
// Act
await view.RenderAsync(viewContext);
// Assert
Assert.IsType<RazorTextWriter>(actual);
Assert.NotSame(original, actual);
}
示例14: RenderAsync_AsPartial_ExecutesLayout_ButNotViewStartPages
public async Task RenderAsync_AsPartial_ExecutesLayout_ButNotViewStartPages()
{
// Arrange
var htmlEncoder = new CommonTestEncoder();
var expected = string.Join(Environment.NewLine,
"HtmlEncode[[layout-content",
"]]HtmlEncode[[page-content]]");
var page = new TestableRazorPage(v =>
{
v.HtmlEncoder = htmlEncoder;
v.Layout = LayoutPath;
v.Write("page-content");
});
var layout = new TestableRazorPage(v =>
{
v.HtmlEncoder = htmlEncoder;
v.Write("layout-content" + Environment.NewLine);
v.RenderBodyPublic();
});
var pageFactory = new Mock<IRazorPageFactory>();
pageFactory.Setup(p => p.CreateInstance(LayoutPath))
.Returns(layout);
var viewEngine = new Mock<IRazorViewEngine>();
viewEngine.Setup(v => v.FindPage(It.IsAny<ActionContext>(), LayoutPath))
.Returns(new RazorPageResult(LayoutPath, layout));
var viewStartProvider = CreateViewStartProvider();
var view = new RazorView(viewEngine.Object,
Mock.Of<IRazorPageActivator>(),
viewStartProvider,
page,
isPartial: true);
var viewContext = CreateViewContext(view);
// Act
await view.RenderAsync(viewContext);
// Assert
Mock.Get(viewStartProvider)
.Verify(v => v.GetViewStartPages(It.IsAny<string>()), Times.Never());
Assert.Equal(expected, viewContext.Writer.ToString());
}
示例15: CreateViewContext
private static ViewContext CreateViewContext(RazorView view)
{
var httpContext = new DefaultHttpContext();
var actionContext = new ActionContext(httpContext, routeData: null, actionDescriptor: null);
return new ViewContext(
actionContext,
view,
new ViewDataDictionary(new EmptyModelMetadataProvider()),
Mock.Of<ITempDataDictionary>(),
new StringWriter(),
new Rendering.HtmlHelperOptions());
}