wdcossey / RazorEngineCore.Extensions

Extensions for RazorEngineCore (ASP.NET Core 3.1.1 Razor Template Engine)
11 stars 5 forks source link

Cast error #7

Closed kobruleht closed 3 years ago

kobruleht commented 3 years ago

Tried to use Html extension support in ASP .NET MVC 6 Core using

           var compiledTemplate = (IRazorEngineCompiledTemplate<RazorEngineCorePageModel<TModel>>)TemplateCache.GetOrAdd(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]]'.

Using original tempales with

var compiledTemplate = (IRazorEngineCompiledTemplate<RazorEngineTemplateBase<TModel>>)TemplateCache.GetOrAdd(templateHashCode, i => works OKJ

adoconnection commented 3 years ago

you need to make TemplateCache to store object private static ConcurrentDictionary<int, object> TemplateCache = new ConcurrentDictionary<int, object>();

https://github.com/adoconnection/RazorEngineCore/issues/28'

🎄