I have the following code:
var viewBag = new ExpandoObject();
((dynamic)viewBag).Title = template.Name;
var key = $"{template.Name}_{template.Content.GetHashCode()}";
StringBuilder content = GenerateContent(template, overlay);
var result = await engine.CompileRenderAsync(key, content.ToString(), model, viewBag);
and when I do:
@ViewBag.Title
in the template. I get:
RuntimeBinderException: 'System.Dynamic.ExpandoObject' does not contain a definition for 'Title'
CallSite.Target(Closure , CallSite , object )
System.Dynamic.UpdateDelegates.UpdateAndExecute1
RazorLight.CompiledTemplates.GeneratedTemplate+
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
RazorLight.TemplateRenderer+
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
RazorLight.TemplateRenderer+
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
RazorLight.TemplateRenderer+
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
RazorLight.TemplateRenderer+
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
RazorLight.RazorLightEngine+
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
RazorLight.RazorLightEngine+
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
RazorLight.RazorLightEngine+
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
System.Runtime.CompilerServices.TaskAwaiter.GetResult()
DataToHtml.Implementations.TemplateCompiler+
+
var result = await engine.CompileRenderAsync(key, content.ToString(), model, viewBag);
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
DataToHtml.Implementations.TemplateCompiler+
+
await Generate(nested, model, builder, overlay);
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
DataToHtml.Implementations.TemplateCompiler+
+
await Generate(template, model, builder, overlay);
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
System.Runtime.CompilerServices.TaskAwaiter.GetResult()
DataToHtml.Controllers.HomeController+
+
var result = await templateCompiler.Process(new Template("parent") {
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
System.Runtime.CompilerServices.TaskAwaiter.GetResult()
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker+
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker+
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ActionExecutedContext context)
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker+
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker+
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker+
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker+
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
Microsoft.AspNetCore.Builder.RouterMiddleware+
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware+
CSharp
var viewBag = new ExpandoObject();
You do not mark your ExpandoObject as dynamic. Try this one
CSharp
dynamic viewBag = new ExpandoObject();
I found the issue. I will make a PR with the necessary fix.
I couldn't get a PR figured out. The problem is in RazorLightEngine.cs, around line 89:
public async Task<string> RenderTemplateAsync(ITemplatePage templatePage, object model, Type modelType, ExpandoObject viewBag = null)
{
using (var writer = new StringWriter())
{
await RenderTemplateAsync(templatePage, model, modelType, writer, viewBag); // viewBag was missing
string result = writer.ToString();
return result;
}
}
This is on 2.0-alpha3 tag/branch (As I am trying to use this in .NET Core 2.0)
A workaround for now is to call RenderTemplateAsync(ITemplatePage, object, Type, TextWriter, ExpandoObject) yourself which is using the viewbag variable.
The code is already fixed, waiting for new release.
Is there a new release yet for this?
When is there going to be a new release? I am held up by this issues as well.
@SeriousM what value do you set for TextWriter when calling ?
RenderTemplateAsync(ITemplatePage, object, Type, TextWriter, ExpandoObject)
@SeriousM what value do you set for TextWriter when calling ?
Sorry but I can't help you with that anymore. It's too long ago.
Most helpful comment
Is there a new release yet for this?