Using @Html and other extesions in runtime compiled templates RRS feed

  • Question

  • User-1262787652 posted

    Looking to render Razor views in code. Views use @HTml. extensions.

    Tried to use https://github.com/wdcossey/RazorEngineCore.Extensions which should add this feature to RazorEngineCore :

        tOrAdd(templateHashCode, i =>
        (IRazorEngineCompiledTemplate<RazorEngineCorePageModel<TModel>>)TemplateCache.GetOrAdd(templateHashCode, i =>
                        var razorEngine = new RazorEngine();
                        var compiledTemplate = razorEngine.Compile<RazorEngineTemplateBase<TModel>>(template, builder =>
                        return compiledTemplate;
    but got exception
        An unhandled exception occurred while processing the request.
        InvalidCastException: Unable to cast object of type 'RazorEngineCore.RazorEngineCompiledTemplate1[RazorEngineCore.RazorEngineTemplateBase1[System.Object]]' to type 'RazorEngineCore.IRazorEngineCompiledTemplate1[RazorEngineCore.RazorEngineCorePageModel1[System.Object]]'.
    Original template (which does not have @Html support):
        var compiledTemplate = (IRazorEngineCompiledTemplate<RazorEngineTemplateBase<TModel>>)TemplateCache.GetOrAdd(templateHashCode, i =>

    works but @Html and other extensions cause error.

    How to use @Html and other extensions in compiled Razor templates in ASP.NET MVC Core 6 ?

    Saturday, January 2, 2021 3:56 PM

All replies