RazorEngine - 如何使用自己的HTML帮助程序?

时间:2015-11-30 23:27:25

标签: asp.net-mvc razorengine

我在RazorEngine模板中有以下内容:

@using WAND.Helpers
@model Tour.Entities.Accommodation

<p>
    You can view your WAND accommodation home page by @Html.HomepageActionLink("Accommodation","clicking here",@Model.AccommodationId)
</p>

HomePageActionLink是我写的一个帮手,部分看起来像这样:

    public static MvcHtmlString HomepageActionLink(this HtmlHelper helper, string serviceProviderType, string linkText, int idValue )
    {
        ...

        return new MvcHtmlString(link);
    }

问题在于,当我运行此操作时,我收到错误消息:

More details about the error: - error: (64, 60) The name 'Html' does not exist in the current context 

我有办法让这个工作吗?

1 个答案:

答案 0 :(得分:1)

您可以使用具有Html实现的类覆盖BaseTemplateType。 像

var config = new TemplateServiceConfiguration();
        // MvcHtmlStringFactory code from 
        // http://stackoverflow.com/questions/19431365/razorengine-html-helpers-work-but-escape-the-html
        config.EncodedStringFactory = new MvcHtmlStringFactory();

        // HtmlTemplateBase code from
        // http://stackoverflow.com/questions/8561164/razorengine-issues-with-html
        config.BaseTemplateType = typeof(HtmlTemplateBase<>);
        var service = RazorEngineService.Create(config);

        var Colmodel ="@Html.BeginForm(\"UploadDocument\", \"Detail\", new{a=1,b=2})";

        string res = service.RunCompile(Colmodel, Colmodel,
            model.GetType(), model);