Display performance on the fly

I am developing an ASP.NET MVC application that will send a confirmation email to the user. For the letter itself, I would like to create a view, and then visualize this view and send it using .NET mail objects.

How to do this using the MVC framework?

+3
source share
3 answers

As in my commentary on Richard, this code did work, but it always led to the error "Unable to redirect after HTTP headers were sent."

After much jerking around Google and frustration, I finally found code that seems to do the trick in this article:

http://mikehadlow.blogspot.com/2008/06/mvc-framework-capturing-output-of-view_05.html

, HttpContext.

MVCContrib BlockRenderer HttpContext , , StringWriter.

( , , ).

+2

IView.Render. , ViewEngineCollection.FindView (ViewEngines.Engines.FindView ). TextWriter , ViewEngine.ReleaseView . ():

StringWriter output = new StringWriter();

string viewName = "Email";
string masterName = "";

ViewEngineResult result = ViewEngines.Engines.FindView(ControllerContext, viewName, masterName);

ViewContext viewContext = new ViewContext(ControllerContext, result.View, viewData, tempData);
result.View.Render(viewContext, output);

result.ViewEngine.ReleaseView(ControllerContext, result.View);

string viewOutput = output.ToString();

viewData/tempData.

+3

This worked for me:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;

namespace Profiteer.WebUI.Controllers
{
    public class SampleController : Controller
    {
        public ActionResult Index()
        {
            RenderViewAsHtml(RouteData.Values["controller"].ToString(), 
                             RouteData.Values["action"].ToString());
            return View();
        }

        private void RenderViewAsHtml(string controllerName, string viewName)
        {
            var vEngine = (from ve in ViewEngineCollection
                           where ve.GetType() == typeof(RazorViewEngine)
                           select ve).FirstOrDefault();
            if (vEngine != null)
            {
                var view = 
                    vEngine.FindView(
                        ControllerContext, 
                        viewName, "_Layout", false).View as RazorView;
                if (view != null)
                {
                    var outPath = 
                       Server.MapPath(
                          string.Format("~/Views/{0}/{1}.html", 
                                        controllerName, viewName));
                    using (var sw = new StreamWriter(outPath, false))
                    {
                        var viewContext = 
                            new ViewContext(ControllerContext, 
                                            view, 
                                            new ViewDataDictionary(), 
                                            new TempDataDictionary(), 
                                            sw);
                        view.Render(viewContext, sw);
                    }
                }
            }
        }
    }
}
0
source

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


All Articles