How do I get the resulting HTML from an MVC action?

Is it possible to get the resulting HTML that is output from the action via code?

+2


source to share


3 answers


Check out MvcIntegrationTestFramework



0


source


To be specific why you are looking at MvcIntegrationTestFramework .

This saves your own streaming helpers and works reasonably well as it turns out. I would assume this will be in a test project and as a bonus you will have other testing options once you get this setup. The main problem is likely to be related to the dependency chaining.



 private static readonly string mvcAppPath = 
     Path.GetFullPath(AppDomain.CurrentDomain.BaseDirectory 
     + "\\..\\..\\..\\MyMvcApplication");
        private readonly AppHost appHost = new AppHost(mvcAppPath);

    [Test]
    public void Root_Url_Renders_Index_View()
    {
        appHost.SimulateBrowsingSession(browsingSession => {
            RequestResult result = browsingSession.ProcessRequest("");
            Assert.IsTrue(result.ResponseText.Contains("<!DOCTYPE html"));
        });
}

      

0


source


Here's an example of how I did it in Razor syntax. I needed to get html from one activity as string in another activity (in pdf rendering)

 ViewResult viewResult = ActionYouWhatHtmlFrom(id);

       using (var writer = new StringWriter())
       {
           ViewEngineResult result = ViewEngines
                     .Engines
                     .FindView(ControllerContext,
                               viewResult.ViewName, "_YourLayout");


           var viewPath = ((RazorView)result.View).ViewPath;
           var layoutPath = ((RazorView) result.View).LayoutPath;

           var view = new RazorView(ControllerContext, viewPath, layoutPath, true, null);
           var viewCxt = new ViewContext(
                               ControllerContext,
                               view,
                               viewResult.ViewData,
                               viewResult.TempData, writer);
           viewCxt.View.Render(viewCxt, writer);

      

0


source







All Articles