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.

Error compiling: The type or namespace name 'Razor' does not exist in the namespace 'RazorLight'...

See original GitHub issue

Describe the bug When attempting to compile a string template and a complex model, we get the error:

Failed to compile generated Razor template:
- (3:29) The type or namespace name 'Razor' does not exist in the namespace 'RazorLight' (are you missing an assembly reference?)

To Reproduce

  • .Net Framework 4.7.2
  • Project is an ASP.NET solution with .cshtml views, which work fine
  • Use a non-trivial string template with references to C# POCOs (a simple “Hello World” string works fine)

Expected behavior

  • The CompileRenderStringAsync() function returns the result

Information (please complete the following information):

  • OS: Windows 10 (dev environment)
  • Platform: .NET Framework 4.7.2
  • RazorLight version: 2.0.0-rc.3 (latest)
  • Are you using the OFFICIAL RazorLight package? https://www.nuget.org/packages/razorlight (I installed it with Package Manager as recommended on your main page)
  • Visual Studio version: VS Professional 2019 Version 16.8.2 (latest)

Additional context Clearly the library is referenced - there would be no way to call the .CompileRenderStringAsync() method otherwise. And it does build and compile fine.

I tried these things:

  1. Added the <PropertyGroup> settings you mentioned (all of them) in the ASP.NET .csproj file. It didn’t change anything - and also those might apply only to .Net Core?

I don’t think this is an issue with your library necessarily - it’s just unclear how to figure this out.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:1
  • Comments:11

github_iconTop GitHub Comments

1reaction
GiantCaiBcommented, Sep 28, 2021

This issue caused by .SetOperatingAssembly(typeof(OurWebApp.Data.Order).Assembly)

If your model is dynamic object, it will give you above errors. Be careful if you just simply remove this, RazorLightEngine.Build() will try to get the Assembly Assembly assembly = operatingAssembly ?? Assembly.GetEntryAssembly(); RoslynCompilationService compilationService = new RoslynCompilationService(referenceManager, assembly);

This will cause null value exception if you run your code in unit test and cross projects.

1reaction
jbaumbachcommented, Jan 5, 2021

Well, that sounds pretty complicated but glad you got to the bottom of it. Let me know when build RazorLight 2.0.0-rc.4 is ready and I’ll check it out.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Razor The type or namespace could not be found
I had this issue with a (ASP).NET Core project. The root cause was because the .csproj file was automagically updated after I mistakenly...
Read more >
RazorLight
EmbeddedResource source. For embedded resource, the key is the namespace of the project where the template exists combined with the template's file name....
Read more >
RazorLight
Failed to compile generated Razor template: - (3:29) The type or namespace name 'Razor' does not exist in the namespace 'RazorLight' (are you...
Read more >
Razor syntax reference for ASP.NET Core
Razor is a markup syntax for embedding .NET based code into webpages. The Razor syntax consists of Razor markup, C#, and HTML. Files...
Read more >
I'm getting this issue: the type or namespace name 'Mvc' ...
I'm getting this issue: the type or namespace name 'Mvc' does not exist in the namespace 'Microsoft.AspnetCore' (are you missing an assembly reference?)...
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