question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

'object' does not contain a definition for 'Name'

See original GitHub issue

I’m trying to convert an old project with RazorEngine to .netcore and RazorLight but with the old working template and code I get always this error: ‘object’ does not contain a definition for ‘Name’

SomeMethod("\r\n<h1>Hello @Model.Name, welcome to RazorEngine!</h1>", new { Name = "pippo" });

.....

public string SomeMethod(string templateText, object model)
{
    var engine = new RazorLightEngineBuilder().UseMemoryCachingProvider().Build();
    Task<string> messageTask = Task.Run(async () => await engine.CompileRenderAsync("TemplateKey", templateText, model));
    var message = messageTask.Result;
    return message;
}

full exception:

"System.AggregateException: One or more errors occurred. ('object' does not contain a definition for 'Name') ---> Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'object' does not contain a definition for 'Name'
   at CallSite.Target(Closure , CallSite , Object )
   at System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,TRet](CallSite site, T0 arg0)
   at RazorLight.CompiledTemplates.GeneratedTemplate.<ExecuteAsync>d__0.MoveNext() in testtemplate:line 2
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at RazorLight.TemplateRenderer.<RenderPageCoreAsync>d__9.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at RazorLight.TemplateRenderer.<RenderPageAsync>d__8.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at RazorLight.TemplateRenderer.<RenderAsync>d__7.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at RazorLight.RazorLightEngine.<RenderTemplateAsync>d__17.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at RazorLight.RazorLightEngine.<RenderTemplateAsync>d__16.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at RazorLight.RazorLightEngine.<CompileRenderAsync>d__11.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at TestProject.Mailer.Domain.MailTemplate.<>c__DisplayClass17_0.<<WithParameters>b__1>d.MoveNext() in D:\\Projects\\TestProject.Mailer\\TestProject.Mailer\\Domain\\MailTemplate.cs:line 61
   --- End of inner exception stack trace ---
   at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)
   at TestProject.Mailer.Domain.MailTemplate.WithParameters(Object model) in D:\\Projects\\TestProject.Mailer\\TestProject.Mailer\\Domain\\MailTemplate.cs:line 62
   at TestProject.Mailer.CoreConsole.Program.Main(String[] args) in D:\\Projects\\TestProject.Mailer\\TestProject.Mailer.CoreConsole\\Program.cs:line 21
---> (Inner Exception #0) Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'object' does not contain a definition for 'Name'
   at CallSite.Target(Closure , CallSite , Object )
   at System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,TRet](CallSite site, T0 arg0)
   at RazorLight.CompiledTemplates.GeneratedTemplate.<ExecuteAsync>d__0.MoveNext() in testtemplate:line 2
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at RazorLight.TemplateRenderer.<RenderPageCoreAsync>d__9.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at RazorLight.TemplateRenderer.<RenderPageAsync>d__8.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at RazorLight.TemplateRenderer.<RenderAsync>d__7.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at RazorLight.RazorLightEngine.<RenderTemplateAsync>d__17.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at RazorLight.RazorLightEngine.<RenderTemplateAsync>d__16.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at RazorLight.RazorLightEngine.<CompileRenderAsync>d__11.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at TestProject.Mailer.Domain.MailTemplate.<>c__DisplayClass17_0.<<WithParameters>b__1>d.MoveNext() in D:\\Projects\\TestProject.Mailer\\TestProject.Mailer\\Domain\\MailTemplate.cs:line 61<---

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:16 (3 by maintainers)

github_iconTop GitHub Comments

15reactions
toddamscommented, Mar 7, 2018

You are casting anonymous object to system.object. Exception message is self explanatory. Don’t generics instead of object in your method. Closing

3reactions
rbnswartzcommented, Oct 3, 2018

@MarcoNicolodi if the class ViewModel isn’t public it breaks. If it is public then everything in the sample works just fine.

Read more comments on GitHub >

github_iconTop Results From Across the Web

'object' does not contain a definition for 'X'
You are using an anonymous object here: ViewBag.Languages = db.Languages .Select(x => new { x.Name, x.EnglishName, x.Id }) .ToList();.
Read more >
How to fix the 'object' does not contain a definition for error
One reason to get 'object' does not contain a definition for...'any property name' exception is that the class you referenced in the ViewBag ......
Read more >
error when try to get values from object parameter ...
I working on blazor web application server side by csharp . I face issue i can't get values of properties employee id and...
Read more >
'object' does not contain a definition for 'AddValue'
When I try and use the AddValue method I receive the 'object does not contain a definition for 'AddValue'. This error is encountered...
Read more >
error CS1061: 'object' does not contain a definition for 'Foo' ...
error CS1061: 'object' does not contain a definition for 'Foo' and no extension method 'Foo' accepting a first argument of type 'object' could...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found