Razorlight: ViewBag - ExpandoObject does not contain a definition for 'Title'

Created on 6 Oct 2017  路  8Comments  路  Source: toddams/RazorLight

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(CallSite site, T0 arg0)
RazorLight.CompiledTemplates.GeneratedTemplate+d__0.MoveNext() in _EmailLayout.cshtml
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
RazorLight.TemplateRenderer+d__9.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
RazorLight.TemplateRenderer+d__8.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
RazorLight.TemplateRenderer+d__11.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
RazorLight.TemplateRenderer+d__7.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
RazorLight.RazorLightEngine+d__12.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
RazorLight.RazorLightEngine+d__11.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
RazorLight.RazorLightEngine+d__9.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
System.Runtime.CompilerServices.TaskAwaiter.GetResult()
DataToHtml.Implementations.TemplateCompiler+d__4.MoveNext() in TemplateCompiler.cs
+
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+d__4.MoveNext() in TemplateCompiler.cs
+
await Generate(nested, model, builder, overlay);
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
DataToHtml.Implementations.TemplateCompiler+d__3.MoveNext() in TemplateCompiler.cs
+
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+d__2.MoveNext() in HomeController.cs
+
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+d__12.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker+d__10.MoveNext()
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+d__14.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker+d__22.MoveNext()
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+d__17.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker+d__15.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
Microsoft.AspNetCore.Builder.RouterMiddleware+d__4.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware+d__7.MoveNext()

bug

Most helpful comment

Is there a new release yet for this?

All 8 comments

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.

Was this page helpful?
0 / 5 - 0 ratings