Skip to content

Instantly share code, notes, and snippets.

View dan-petru's full-sized avatar
💭
Projects enthusiast, fighting complexity and striving for quality.

Dan Petru dan-petru

💭
Projects enthusiast, fighting complexity and striving for quality.
View GitHub Profile
@dan-petru
dan-petru / RazorHelpers.cs
Created August 28, 2018 12:23 — forked from sniffdk/RazorHelpers.cs
Snippets showing how to render razor files with WebPages and MCV
public class RazorHelpers
{
// This approach is using WebPages, where there is no concept of a Controller
public static string RenderTemplate(string template, object model)
{
var page = WebPageBase.CreateInstanceFromVirtualPath("~/templates/" + template + ".cshtml");
var context = new WebPageContext(new HttpContextWrapper(HttpContext.Current), page, null);
var htmlWriter = new StringWriter();
// Access your model through PageData["Model"] in the view
context.PageData["Model"] = model;