Repository navigation
@Html implementation example
Alexander Selishchev edited this page Oct 22, 2026
·
3 revisions
@Html is a part of MVC, therefore it is absent in RazorEngine. @Html does a lot of things internally, like accessing ViewData, routes etc., so you can't use it by simply referencing the AspNetCore.Mvc namespace.
In order to use @Html in a template, the template base must contain an initialized Html property with your own helper, like public MyHtmlHelper<T> Html { get; set; } below.
public class TestModel
{
public string StudentId { get; set; }
}
public class MvcTemplate<T> : RazorEngineTemplateBase<T>
{
public MyHtmlHelper<T> Html { get; set; }
}
public class MyHtmlHelper<T>
{
private readonly MvcTemplate<T> templateInstance;
public MyHtmlHelper(MvcTemplate<T> templateInstance)
{
this.templateInstance = templateInstance;
}
public string Hidden(Expression<Func<T, object>> selector)
{
object propertyValue = selector.Compile()(this.templateInstance.Model);
MemberExpression body = (MemberExpression)selector.Body;
string name = HttpUtility.HtmlAttributeEncode(body.Member.Name);
string value = HttpUtility.HtmlAttributeEncode(propertyValue?.ToString() ?? "");
return $"<input type=\"hidden\" name=\"{name}\" value=\"{value}\" />";
}
}
class Program
{
static void Main(string[] args)
{
string content = @"
<p>hidden: @Html.Hidden(m => m.StudentId)</p>
";
RazorEngine razorEngine = new RazorEngine();
var compiledTemplate = razorEngine.Compile<MvcTemplate<TestModel>>(content);
string result = compiledTemplate.Run(template =>
{
template.Model = new TestModel()
{
StudentId = "<script>alert(\"1\");</script>"
};
template.Html = new MyHtmlHelper<TestModel>(template);
});
Console.WriteLine(result);
Console.ReadKey();
}
}Output:
<p>hidden: <input type="hidden" name="StudentId" value="<script>alert("1");</script>" /></p>