How to view the resulting html controller in debugging using ASP.NET MVC?

Is there any way that I can see, when debugging, the result (html, xml, json) of the Controller action.

For example, I have this in the controller:

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

During debugging, I would like to see what HTML code will be sent to the client. Thanks

+3
source share
3 answers

This is not easy, because the result of the partial view is sent directly to httpcontext.current.response.output, it does not return a string. You can use this extension method to catch it as a string by filtering the output of httpcontext:

    /// <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 during debugging:

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();
    }
+1
source

, . , . JSON , Firefox + Firebug, .

, , ...

, , . , RazorViewEngine. , , (.. RazorView), , , : .

, , . HTML , , ? , - MVC Asp.net.

Asp.net MVC?

Asp.net MVC...

, c.Controls.Add(), , :

  • - ; , , ; , , , factory;

  • ASPX/ASCX/CSHTML :

    • - , HTML ( ), ;

    • Razor , -; Razor - , , , ; ...

: . , , . .

+2

, ?

+1

Source: https://habr.com/ru/post/1720361/