// Render Razor/Aspx to string and output stream protected string RenderRazorToString <TModel>(ExceptionContext filterContext, ViewEngineType viewType, string viewName, TModel model) { var ret = string.Empty; ViewData.Model = model; using (var writer = new StringWriter()) { switch (viewType) { case ViewEngineType.Razor: var viewEngineResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName); var viewContext = new ViewContext(ControllerContext, viewEngineResult.View, ViewData, TempData, writer); viewEngineResult.View.Render(viewContext, writer); viewEngineResult.ViewEngine.ReleaseView(ControllerContext, viewEngineResult.View); ret = writer.GetStringBuilder().ToString(); break; case ViewEngineType.Aspx: default: var view = new WebFormView(ControllerContext, viewName); var vdd = new ViewDataDictionary <TModel>(model); var viewCxt = new ViewContext(ControllerContext, view, vdd, new TempDataDictionary(), writer); viewCxt.View.Render(viewCxt, writer); ret = writer.ToString(); break; } } return(ret); }
public void SetUp() { path = new ViewPath { ViewName = "something" }; var render = new WebFormView(path.ViewName); var container = new Container(x => { x.For<IActionBehavior>().Use(new ObjectDefInstance(render.As<IContainerModel>().ToObjectDef(DiagnosticLevel.None))); x.For<IWebFormsControlBuilder>().Use<WebFormsControlBuilder>(); x.For<IWebFormRenderer>().Use<WebFormRenderer>(); x.For<HttpContextBase>().Use(() => new FakeHttpContext()); x.For<IOutputWriter>().Use<OutputWriter>(); x.For<IFubuRequest>().Use<InMemoryFubuRequest>(); x.For<IPageActivator>().Use<PageActivator>(); x.For<IPageActivationRules>().Use<PageActivationRuleCache>(); x.For<IServiceLocator>().Use<StructureMapServiceLocator>(); x.For<IHttpWriter>().Use(new NulloHttpWriter()); x.For<IFileSystem>().Use<FileSystem>(); }); behavior = container.GetInstance<IActionBehavior>(); }
public void description_should_contain_view_name() { WebFormView view = token.ToBehavioralNode() as WebFormView; view.ShouldNotBeNull(); view.Description.ShouldEqual("WebForm View '{0}'".ToFormat("~/View/View4.aspx")); }
public void RenderWithViewPageRendersView() { // Arrange ViewContext context = new Mock <ViewContext>().Object; MockBuildManager buildManager = new MockBuildManager("view path", typeof(object)); Mock <IViewPageActivator> activator = new Mock <IViewPageActivator>(MockBehavior.Strict); ControllerContext controllerContext = new ControllerContext(); StubViewPage viewPage = new StubViewPage(); activator.Setup(l => l.Create(controllerContext, typeof(object))).Returns(viewPage); WebFormView view = new WebFormView( controllerContext, "view path", null, activator.Object ); view.BuildManager = buildManager; // Act view.Render(context, null); // Assert Assert.Equal(context, viewPage.ResultViewContext); Assert.Equal(String.Empty, viewPage.MasterLocation); }
public void SetUp() { path = new ViewPath { ViewName = "something" }; var render = new WebFormView(path.ViewName); var container = new Container(x => { x.For <IActionBehavior>().Use(new ObjectDefInstance(render.As <IContainerModel>().ToObjectDef(DiagnosticLevel.None))); x.For <IWebFormsControlBuilder>().Use <WebFormsControlBuilder>(); x.For <IWebFormRenderer>().Use <WebFormRenderer>(); x.For <HttpContextBase>().Use(() => new FakeHttpContext()); x.For <IOutputWriter>().Use <OutputWriter>(); x.For <IFubuRequest>().Use <InMemoryFubuRequest>(); x.For <IPageActivator>().Use <PageActivator>(); x.For <IPageActivationRules>().Use <PageActivationRuleCache>(); x.For <IServiceLocator>().Use <StructureMapServiceLocator>(); x.For <IHttpWriter>().Use(new NulloHttpWriter()); x.For <IFileSystem>().Use <FileSystem>(); }); behavior = container.GetInstance <IActionBehavior>(); }
public void MasterPathProperty() { // Act WebFormView view = new WebFormView(new ControllerContext(), "view path", "master path"); // Assert Assert.AreEqual("master path", view.MasterPath); }
public void MasterPathPropertyReturnsEmptyStringIfMasterNotSpecified() { // Act WebFormView view = new WebFormView(new ControllerContext(), "view path", null); // Assert Assert.AreEqual(String.Empty, view.MasterPath); }
public void MasterPathProperty() { // Act WebFormView view = new WebFormView(new ControllerContext(), "view path", "master path"); // Assert Assert.Equal("master path", view.MasterPath); }
public void ViewPathProperty() { // Act WebFormView view = new WebFormView("view path", "master path"); // Assert Assert.AreEqual("view path", view.ViewPath); }
public void MasterPathPropertyReturnsEmptyStringIfMasterNotSpecified() { // Act WebFormView view = new WebFormView(new ControllerContext(), "view path", null); // Assert Assert.Equal(String.Empty, view.MasterPath); }
public void create_from_view_type() { var view = new WebFormView(typeof(FakeWebView)); view.ViewName.ShouldEqual(typeof(FakeWebView).ToVirtualPath()); view.InputType.ShouldEqual(typeof(InputModel)); view.As <IMayHaveInputType>().InputType().ShouldEqual(typeof(InputModel)); }
private static string GetVirtualPath(HtmlHelper htmlhelper) { WebFormView view = htmlhelper.ViewContext.View as WebFormView; if (view != null) { return(view.ViewPath); } return(null); }
public ChainedBehaviorExpression RenderWebForm <TWebform>() { // TODO -- blow up if the web form T is strongly typed to something that // does not match the output type of the output type // Do it in validation rules var node = new WebFormView(typeof(TWebform)); return(returnChain(node)); }
public static string RenderViewToString <T>(string viewPath, T model, System.Web.Mvc.ControllerContext controllerContext) { using (var writer = new StringWriter()) { var view = new WebFormView(controllerContext, viewPath); var vdd = new ViewDataDictionary <T>(model); var viewCxt = new ViewContext(controllerContext, view, vdd, new TempDataDictionary(), writer); viewCxt.View.Render(viewCxt, writer); return(writer.ToString()); } }
protected string RenderViewToString() { using (var writer = new StringWriter()) { var view = new WebFormView(_context, _viewPath); var vdd = new ViewDataDictionary <Model>(_model); var viewCxt = new ViewContext(_context, view, vdd, new TempDataDictionary(), writer); viewCxt.View.Render(viewCxt, writer); return(writer.ToString()); } }
public void RenderWithNullContextThrows() { // Arrange WebFormView view = new WebFormView("~/view", "~/master"); // Act & Assert ExceptionHelper.ExpectArgumentNullException( () => view.Render(null, new Mock <TextWriter>().Object), "viewContext" ); }
protected string RenderViewToString <T>(string viewPath, T model) { ViewData.Model = model; using (var writer = new StringWriter()) { var view = new WebFormView(ControllerContext, viewPath); var vdd = new ViewDataDictionary <T>(model); var viewCxt = new ViewContext(ControllerContext, view, vdd, new TempDataDictionary(), writer); viewCxt.View.Render(viewCxt, writer); return(writer.ToString()); } }
/// <summary> /// Returns html of ascx object. /// </summary> /// <typeparam name="T">Type of te model to instaniate view data dictionary</typeparam> /// <param name="viewPath">Path of the ascx file to render.</param> /// <param name="model">Model to pas .ascx</param> /// <param name="masterPagePath">Maser page to render in returned html.</param> /// <returns>Returns html in string.</returns> public static string RenderFormViewToString <T>(this System.Web.Mvc.ControllerBase controller, string viewPath, T model, string masterPagePath) { controller.ViewData.Model = model; using (var writer = new StringWriter()) { var view = new WebFormView(controller.ControllerContext, viewPath, masterPagePath); var vdd = new ViewDataDictionary <T>(model); var viewCxt = new ViewContext(controller.ControllerContext, view, vdd, new TempDataDictionary(), writer); viewCxt.View.Render(viewCxt, writer); return(writer.ToString()); } }
public void CreatePartialViewCreatesWebFormView() { // Arrange TestableWebFormViewEngine engine = new TestableWebFormViewEngine(); // Act WebFormView result = (WebFormView)engine.CreatePartialView("partial path"); // Assert Assert.Equal("partial path", result.ViewPath); Assert.Equal(String.Empty, result.MasterPath); }
public void CreateViewCreatesWebFormView() { // Arrange TestableWebFormViewEngine engine = new TestableWebFormViewEngine(); // Act WebFormView result = (WebFormView)engine.CreateView("view path", "master path"); // Assert Assert.Equal("view path", result.ViewPath); Assert.Equal("master path", result.MasterPath); }
public void CreateView_PassesViewPageActivator() { // Arrange Mock <IViewPageActivator> viewPageActivator = new Mock <IViewPageActivator>(); TestableWebFormViewEngine viewEngine = new TestableWebFormViewEngine(viewPageActivator.Object); // Act WebFormView result = (WebFormView)viewEngine.CreateView("partial path", "master path"); // Assert Assert.Equal(viewEngine.ViewPageActivator, result.ViewPageActivator); }
public void RenderWithUnsupportedTypeThrows() { // Arrange ViewContext context = new Mock<ViewContext>().Object; MockBuildManager buildManagerMock = new MockBuildManager("view path", typeof(int)); WebFormView view = new WebFormView(new ControllerContext(), "view path", null); view.BuildManager = buildManagerMock; // Act & Assert Assert.Throws<InvalidOperationException>( () => view.Render(context, null), "The view at 'view path' must derive from ViewPage, ViewPage<TModel>, ViewUserControl, or ViewUserControl<TModel>." ); }
public void RenderWithViewUserControlAndMasterThrows() { // Arrange ViewContext context = new Mock <ViewContext>().Object; MockBuildManager buildManagerMock = new MockBuildManager("view path", typeof(StubViewUserControl)); WebFormView view = new WebFormView(new ControllerContext(), "view path", "master path"); view.BuildManager = buildManagerMock; // Act & Assert ExceptionHelper.ExpectException <InvalidOperationException>( () => view.Render(context, null), "A master name cannot be specified when the view is a ViewUserControl." ); }
public void RenderWithUnsupportedTypeThrows() { // Arrange ViewContext context = new Mock <ViewContext>().Object; MockBuildManager buildManagerMock = new MockBuildManager("view path", typeof(int)); WebFormView view = new WebFormView(new ControllerContext(), "view path", null); view.BuildManager = buildManagerMock; // Act & Assert ExceptionHelper.ExpectException <InvalidOperationException>( () => view.Render(context, null), "The view at 'view path' must derive from ViewPage, ViewPage<TModel>, ViewUserControl, or ViewUserControl<TModel>." ); }
private static Type GetWebFormViewModelType(WebFormView view) { var viewPage = BuildManager.CreateInstanceFromVirtualPath(view.ViewPath, typeof(object)); if (viewPage == null) { return(null); } var modelType = GetModelType(viewPage); TryDispose(viewPage); return(modelType); }
public void RenderWithNullViewInstanceThrows() { // Arrange ViewContext context = new Mock <ViewContext>().Object; MockBuildManager buildManager = new MockBuildManager("view path", typeof(object), null); WebFormView view = new WebFormView("view path", null); view.BuildManager = buildManager; // Act & Assert ExceptionHelper.ExpectException <InvalidOperationException>( () => view.Render(context, null), "The view found at 'view path' could not be created." ); }
private static void ExecuteTest(ControllerContext testControllerContext, string viewName, string viewPathExpected) { var testViewFormView = new WebFormView(testControllerContext, viewPathExpected); var fakeViewEngine = new Mock <IViewEngine>(); var razorViewEngine = new MockMobileCapableRazorViewEngine( testViewFormView, fakeViewEngine.Object, viewPathExpected); var viewEngineResult = razorViewEngine.FindView(testControllerContext, viewName, string.Empty, false); Assert.IsNotNull(viewEngineResult); Assert.IsNotNull(viewEngineResult.View); Assert.IsNotNull(viewEngineResult.ViewEngine); Assert.AreEqual <string>(viewPathExpected, ((WebFormView)viewEngineResult.View).ViewPath); }
public void RenderWithViewUserControlRendersView() { // Arrange ViewContext context = new Mock <ViewContext>().Object; StubViewUserControl viewUserControl = new StubViewUserControl(); MockBuildManager buildManagerMock = new MockBuildManager("view path", typeof(object), viewUserControl); WebFormView view = new WebFormView("view path", null); view.BuildManager = buildManagerMock; // Act view.Render(context, null); // Assert Assert.AreEqual(context, viewUserControl.ResultViewContext); }
/// <summary> /// Used as an alternative to returning the View() method on MVC Controller Actions. /// Renders an MVC Razor View using a Web Forms Master Page. /// </summary> /// <param name="viewName">The View that you want to render (leave blank or null for the default View).</param> /// <param name="model">The model to pass to the view.</param> /// <returns>A ViewResult for use in an MVC Controller Action.</returns> /// <remarks> /// Include this line in a Web Form Content Page named RazorView.aspx: /// <![CDATA[<% Html.RenderPartial((string)ViewBag._ViewName); %>]]> /// </remarks> public static ViewResult RazorMasterPageView(this Controller controller, string razorRenderingView, string viewName = null, object model = null) { if (model != null) { controller.ViewData.Model = model; } controller.ViewBag._ViewName = viewName.IsNotEmptyOrWhiteSpace() ? viewName : controller.RouteData.GetView(); var razorView = new WebFormView(controller.ControllerContext, razorRenderingView); return(new ViewResult { View = razorView, ViewData = controller.ViewData, TempData = controller.TempData }); }
public void RenderWithViewPageRendersView() { // Arrange ViewContext context = new Mock <ViewContext>().Object; StubViewPage viewPage = new StubViewPage(); MockBuildManager buildManager = new MockBuildManager("view path", typeof(object), viewPage); WebFormView view = new WebFormView("view path", null); view.BuildManager = buildManager; // Act view.Render(context, null); // Assert Assert.AreEqual(context, viewPage.ResultViewContext); Assert.AreEqual(String.Empty, viewPage.MasterLocation); }
public void RenderWithViewPageAndMasterRendersView() { // Arrange ViewContext context = new Mock<ViewContext>().Object; MockBuildManager buildManager = new MockBuildManager("view path", typeof(object)); Mock<IViewPageActivator> activator = new Mock<IViewPageActivator>(MockBehavior.Strict); ControllerContext controllerContext = new ControllerContext(); StubViewPage viewPage = new StubViewPage(); activator.Setup(l => l.Create(controllerContext, typeof(object))).Returns(viewPage); WebFormView view = new WebFormView(controllerContext, "view path", "master path", activator.Object); view.BuildManager = buildManager; // Act view.Render(context, null); // Assert Assert.Equal(context, viewPage.ResultViewContext); Assert.Equal("master path", viewPage.MasterLocation); }
public void SetUp() { path = new ViewPath { ViewName = "something" }; var render = new WebFormView(path.ViewName); var container = new Container(x => { x.For <IActionBehavior>().Use(new ObjectDefInstance(render.ToObjectDef())); x.For <IWebFormsControlBuilder>().Use <WebFormsControlBuilder>(); x.For <IWebFormRenderer>().Use <WebFormRenderer>(); x.For <IOutputWriter>().Use <HttpResponseOutputWriter>(); x.For <IFubuRequest>().Use <InMemoryFubuRequest>(); x.For <IViewActivator>().Use <NulloViewActivator>(); }); behavior = container.GetInstance <IActionBehavior>(); }
public void RenderWithViewUserControlRendersView() { // Arrange ViewContext context = new Mock <ViewContext>().Object; MockBuildManager buildManager = new MockBuildManager("view path", typeof(object)); Mock <IViewPageActivator> activator = new Mock <IViewPageActivator>(MockBehavior.Strict); ControllerContext controllerContext = new ControllerContext(); StubViewUserControl viewUserControl = new StubViewUserControl(); activator.Setup(l => l.Create(controllerContext, typeof(object))).Returns(viewUserControl); WebFormView view = new WebFormView(controllerContext, "view path", null, activator.Object) { BuildManager = buildManager }; // Act view.Render(context, null); // Assert Assert.AreEqual(context, viewUserControl.ResultViewContext); }
public void SetUp() { path = new ViewPath { ViewName = "something" }; var render = new WebFormView(path.ViewName); var container = new Container(x => { x.For<IActionBehavior>().Use(new ObjectDefInstance(render.As<IContainerModel>().ToObjectDef())); x.For<IWebFormsControlBuilder>().Use<WebFormsControlBuilder>(); x.For<IWebFormRenderer>().Use<WebFormRenderer>(); x.For<IOutputWriter>().Use<HttpResponseOutputWriter>(); x.For<IFubuRequest>().Use<InMemoryFubuRequest>(); x.For<IPageActivator>().Use<PageActivator>(); x.For<IPageActivationRules>().Use<PageActivationRuleCache>(); x.For<IServiceLocator>().Use<StructureMapServiceLocator>(); }); behavior = container.GetInstance<IActionBehavior>(); }
public static string RenderViewToString <T>(this Controller controller, string viewPath, T model) { controller.ViewData.Model = model; using (var writer = new StringWriter()) { IView view; if (viewPath.EndsWith("html", StringComparison.OrdinalIgnoreCase)) { view = new RazorView(controller.ControllerContext, viewPath, null, false, null); } else { view = new WebFormView(controller.ControllerContext, viewPath); } var dataDict = new ViewDataDictionary <T>(model); var tempDict = new TempDataDictionary(); var viewContext = new ViewContext(controller.ControllerContext, view, dataDict, controller.TempData /*new TempDataDictionary()*/, writer); viewContext.View.Render(viewContext, writer); return(writer.ToString()); } }
public static string RenderViewToString <T>(this ControllerBase controller, string viewName, T model) { using (var writer = new StringWriter()) { ViewEngineResult result = ViewEngines .Engines .FindView(controller.ControllerContext, viewName, null); var viewPath = ((WebFormView)result.View).ViewPath; var view = new WebFormView(viewPath); var vdd = new ViewDataDictionary <T>(model); var viewCxt = new ViewContext( controller.ControllerContext, view, vdd, new TempDataDictionary(), writer); viewCxt.View.Render(viewCxt, writer); return(writer.ToString()); } }
public void RenderWithViewUserControlRendersView() { // Arrange ViewContext context = new Mock<ViewContext>().Object; MockBuildManager buildManager = new MockBuildManager("view path", typeof(object)); Mock<IViewPageActivator> activator = new Mock<IViewPageActivator>(MockBehavior.Strict); ControllerContext controllerContext = new ControllerContext(); StubViewUserControl viewUserControl = new StubViewUserControl(); activator.Setup(l => l.Create(controllerContext, typeof(object))).Returns(viewUserControl); WebFormView view = new WebFormView(controllerContext, "view path", null, activator.Object) { BuildManager = buildManager }; // Act view.Render(context, null); // Assert Assert.Equal(context, viewUserControl.ResultViewContext); }
public void RenderWithViewUserControlAndMasterThrows() { // Arrange ViewContext context = new Mock<ViewContext>().Object; MockBuildManager buildManagerMock = new MockBuildManager("view path", typeof(StubViewUserControl)); WebFormView view = new WebFormView(new ControllerContext(), "view path", "master path"); view.BuildManager = buildManagerMock; // Act & Assert Assert.Throws<InvalidOperationException>( () => view.Render(context, null), "A master name cannot be specified when the view is a ViewUserControl." ); }