RazorEngine - 如何使用我自己的 HTML 助手?

RazorEngine - How do I use my own HTML helper?

我在 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 

有什么方法可以让它发挥作用吗?

您可以使用具有 Html 实现的 class 覆盖 BaseTemplateType。 喜欢

var config = new TemplateServiceConfiguration();
        // MvcHtmlStringFactory code from 
        // 
        config.EncodedStringFactory = new MvcHtmlStringFactory();

        // HtmlTemplateBase code from
        // 
        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);