tags:

views:

49

answers:

3

Is there a way that I can see, while debugging, the result (html, xml, json) of a Controller's action.

For instance, I have this in my controller:

    public PartialViewResult Reload()
    {
        return PartialView("Index");
    }

While debugging, I'd like to see what HTML will be sent back to the client. Thanks

A: 

It is not easy, because partial view result is sent directly to httpcontext.current.response.output, it doesn't return string. You can use this extension method to catch it as string by filtering httpcontext output:

    /// <summary>Renders a view to string.</summary>
    public static string RenderViewToString(this Controller controller,
                                            string viewName, object viewData)
    {
        //Getting current response
        var response = HttpContext.Current.Response;
        //Flushing
        response.Flush();

        //Finding rendered view
        var view = ViewEngines.Engines.FindPartialView(controller.ControllerContext, viewName).View;
        //Creating view context
        var viewContext = new ViewContext(controller.ControllerContext, view,
                                          controller.ViewData, controller.TempData);

        //Since RenderView goes straight to HttpContext.Current, we have to filter and cut out our view
        var oldFilter = response.Filter;
        Stream filter = new MemoryStream(); ;
        try
        {
            response.Filter = filter;
            viewContext.View.Render(viewContext, null);
            response.Flush();
            filter.Position = 0;
            var reader = new StreamReader(filter, response.ContentEncoding);
            return reader.ReadToEnd();
        }
        finally
        {
            filter.Dispose();
            response.Filter = oldFilter;
        } 
    }

and use it like this during debug:

public PartialViewResult Reload()
{
    var result = RenderViewToString("Index",ViewData);
    return PartialView("Index");
}

Extension method for Spark:

    public static string RenderSparkToString(this Controller controller,
                                            string viewName, object viewData)
    {
        var view = ViewEngines.Engines.FindPartialView(controller.ControllerContext, viewName).View;
        //Creating view context
        var viewContext = new ViewContext(controller.ControllerContext, view,
                                          controller.ViewData, controller.TempData);

        var sb = new StringBuilder();
        var writer = new StringWriter(sb);

        viewContext.View.Render(viewContext, writer);
        writer.Flush();
        return sb.ToString();
    }
LukLed
Thanks. How do I to pass the Model instance to the view in your example? Should I explicitly assign this.ViewData.Model before I invoke the helper?
Sly
Yes, you have to set Model in ViewData. Please copy my function again, because there was an error.
LukLed
Thanks, it works. But for some reason it does not work with the Spark View Engine. A NullReferenceException is thrown when Spark tries to use the TextWriter. In your code, you pass null as the TextWriter argument (viewContext.View.Render(viewContext, null);).
Sly
I don't have experience with Spark view engine. You should look for different solution, because Spark has propably easier ways to render view to string. In Spark You should create Your own TextWriter, Render view to it, and then read from there. There is propably no need to filter HttpContext.Current.Response.
LukLed
Thank. I'm accepting your answer as your solution works witht he defaut MVC View Engine. I thought it would be the same thing with Spark. I'll research how to do the same with Spark. Thanks again.
Sly
As I said earlier, Spark makes it easier. I've added another extension method.
LukLed
+1  A: 

What prevents you from just hitting the Route that renders the partial view and view source?

chakrit
+2  A: 

Browser will show the result anyway

Why would you want to see it in debug mode, while you can see it in your browser anyway. That's where it goes afterwards anyway. If JSON is in question use Firefox + Firebug and you'll see all requests and responses with all related data and info.

If you tell us why exactly would you like to do this, we may suggest a better way of getting you there...

Robert Koritnik