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

后端 未结 15 2967
挽巷
挽巷 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 04:57

    If you want to forgo MVC entirely, thereby avoiding all the HttpContext mess...

    using RazorEngine;
    using RazorEngine.Templating; // For extension methods.
    
    string razorText = System.IO.File.ReadAllText(razorTemplateFileLocation);
    string emailBody = Engine.Razor.RunCompile(razorText, "templateKey", typeof(Model), model);
    

    This uses the awesome open source Razor Engine here: https://github.com/Antaris/RazorEngine

提交回复
热议问题