ASP.NET MVC Email

后端 未结 7 1114
日久生厌
日久生厌 2021-01-30 11:50

Is their a solution to generate an email template using an ASP.NET MVC View without having to jump through hoops.

Let me elaborate jumping through hoops.



        
7条回答
  •  温柔的废话
    2021-01-30 12:32

    I created an overload to LukLed's RenderSparkToString method that allows you to use a spark layout along with your view:

    public static string RenderSparkToString(this Controller controller,
                                            string viewName, string masterName, object viewData)
    {
        var view = ViewEngines.Engines.FindView(controller.ControllerContext, viewName, masterName).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();
    }
    

    I agree with Andrew though. I wish there was an easier way to do this with the web forms view engine.

提交回复
热议问题