How to render an ASP.NET MVC view as a string?

后端 未结 15 2965
挽巷
挽巷 2020-11-21 04:40

I want to output two different views (one as a string that will be sent as an email), and the other the page displayed to a user.

Is this possible in ASP.NET MVC bet

15条回答
  •  失恋的感觉
    2020-11-21 05:04

    To render a view to a string in the Service Layer without having to pass ControllerContext around, there is a good Rick Strahl article here http://www.codemag.com/Article/1312081 that creates a generic controller. Code summary below:

    // Some Static Class
    public static string RenderViewToString(ControllerContext context, string viewPath, object model = null, bool partial = false)
    {
        // first find the ViewEngine for this view
        ViewEngineResult viewEngineResult = null;
        if (partial)
            viewEngineResult = ViewEngines.Engines.FindPartialView(context, viewPath);
        else
            viewEngineResult = ViewEngines.Engines.FindView(context, viewPath, null);
    
        if (viewEngineResult == null)
            throw new FileNotFoundException("View cannot be found.");
    
        // get the view and attach the model to view data
        var view = viewEngineResult.View;
        context.Controller.ViewData.Model = model;
    
        string result = null;
    
        using (var sw = new StringWriter())
        {
            var ctx = new ViewContext(context, view, context.Controller.ViewData, context.Controller.TempData, sw);
            view.Render(ctx, sw);
            result = sw.ToString();
        }
    
        return result;
    }
    
    // In the Service Class
    public class GenericController : Controller
    { }
    
    public static T CreateController(RouteData routeData = null) where T : Controller, new()
    {
        // create a disconnected controller instance
        T controller = new T();
    
        // get context wrapper from HttpContext if available
        HttpContextBase wrapper;
        if (System.Web.HttpContext.Current != null)
            wrapper = new HttpContextWrapper(System.Web.HttpContext.Current);
        else
            throw new InvalidOperationException("Cannot create Controller Context if no active HttpContext instance is available.");
    
        if (routeData == null)
            routeData = new RouteData();
    
        // add the controller routing if not existing
        if (!routeData.Values.ContainsKey("controller") &&
            !routeData.Values.ContainsKey("Controller"))
            routeData.Values.Add("controller", controller.GetType().Name.ToLower().Replace("controller", ""));
    
        controller.ControllerContext = new ControllerContext(wrapper, routeData, controller);
        return controller;
    }
    

    Then to render the View in the Service class:

    var stringView = RenderViewToString(CreateController().ControllerContext, "~/Path/To/View/Location/_viewName.cshtml", theViewModel, true);
    

提交回复
热议问题