RazorLight
RazorLight copied to clipboard
The type or namespace name 'Razor' does not exist in the namespace 'RazorLight'
Describe the bug We are trying to migrate a .NET Framework solution to .Net core/.Net 6. In doing so, our infrastructure projects in the solution that power our 2 web application projects have been converted to .Net Standard 2 so we can have the updates live while migrating the web apps. In one of the .Net Standard 2 projects, we need to compile .cshtml views with models so I tried to use RazorLight. I've had to install a number of nuget packages in the web app but have finally gotten past all of those exceptions and now I'm at, what I hope is, the last of the exceptions. When I try to use the CompileRenderStringAsync as below I get the error "The type or namespace name 'Razor' does not exist in the namespace 'RazorLight'".
public string GenerateHtmlFromRazorTemplate<T>(string viewPath, T model)
{
var engine = new RazorLightEngineBuilder()
.UseEmbeddedResourcesProject(typeof(T))
.SetOperatingAssembly(typeof(T).Assembly)
.UseMemoryCachingProvider()
.Build();
var viewTemplate = EmbeddedResourceHelper.GetEmbdeddedResource(viewPath);
return Task.Run(() => engine.CompileRenderStringAsync(nameof(T), viewTemplate, model)).Result;
}
To Reproduce Create a .Net Standard 2 library referencing RazorLight. Create a .Net Frameworks 4.7.2 web app and reference the first library app. Create a call through a button or something in the web app that tries to call the .Net Standard's implementation of RazorLight.
Expected behavior Expecting... or hoping, to be able to compile .cshtml views.
Information (please complete the following information):
- OS: Windows 10
- Platform .NET Framework 4.7.2, .NET Standard 2
- RazorLight version 2.3.1
- Are you using the OFFICIAL RazorLight package? yes
- Visual Studio version Professional 2022
Bumping in hopes someone has an idea.
I'm getting the same error in a .NET 7.0 project.
Here is the message: `RazorLight.Compilation.TemplateCompilationException: '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?)
- (14:56) The type or namespace name 'TemplatePage<>' does not exist in the namespace 'RazorLight' (are you missing an assembly reference?)
- (17:66) 'GeneratedTemplate.ExecuteAsync()': no suitable method found to override
- (19:12) The name 'WriteLiteral' does not exist in the current context
- (6:6) The name 'Write' does not exist in the current context
- (6:12) The name 'Model' does not exist in the current context
- (27:12) The name 'WriteLiteral' does not exist in the current context
- (42:12) The name 'WriteLiteral' does not exist in the current context
- (23:23) The name 'Write' does not exist in the current context
- (23:29) The name 'Html' does not exist in the current context
- (23:38) The name 'Model' does not exist in the current context
- (53:12) The name 'WriteLiteral' does not exist in the current context
- (28:50) The name 'Model' does not exist in the current context
- (67:12) The name 'WriteLiteral' does not exist in the current context
- (68:12) The name 'BeginWriteAttribute' does not exist in the current context
- (32:0) The name 'WriteAttributeValue' does not exist in the current context
- (32:30) The name 'Model' does not exist in the current context
- (76:12) The name 'EndWriteAttribute' does not exist in the current context
- (77:12) The name 'WriteLiteral' does not exist in the current context
- (78:12) The name 'BeginWriteAttribute' does not exist in the current context
- (33:0) The name 'WriteAttributeValue' does not exist in the current context
- (86:12) The name 'EndWriteAttribute' does not exist in the current context
- (87:12) The name 'WriteLiteral' does not exist in the current context
- (35:35) The name 'Write' does not exist in the current context
- (95:12) The name 'WriteLiteral' does not exist in the current context
- (42:104) The name 'Write' does not exist in the current context
- (110:12) The name 'WriteLiteral' does not exist in the current context
- (42:114) The name 'Write' does not exist in the current context
- (118:12) The name 'WriteLiteral' does not exist in the current context
- (45:104) The name 'Write' does not exist in the current context
- (126:12) The name 'WriteLiteral' does not exist in the current context
- (51:94) The name 'Write' does not exist in the current context
- (140:12) The name 'WriteLiteral' does not exist in the current context
- (141:12) The name 'BeginWriteAttribute' does not exist in the current context
- (54:0) The name 'WriteAttributeValue' does not exist in the current context
- (54:30) The name 'Model' does not exist in the current context
- (149:12) The name 'EndWriteAttribute' does not exist in the current context
- (150:12) The name 'WriteLiteral' does not exist in the current context
- (158:12) The name 'WriteLiteral' does not exist in the current context
- (165:12) The name 'BeginWriteAttribute' does not exist in the current context
- (67:0) The name 'WriteAttributeValue' does not exist in the current context
- (67:30) The name 'Model' does not exist in the current context
- (67:65) The name 'Model' does not exist in the current context
- (173:12) The name 'EndWriteAttribute' does not exist in the current context
- (174:12) The name 'WriteLiteral' does not exist in the current context`
Hi,
I have the same error with my cshtml in a .NET standard 2.0 class library used by a .NET 4.6.2 console application. Any idea ?
Regards Sybaris
Here the code of my Console Application (.NET 4.6.2) :
internal class Program
{
static void Main(string[] args)
{
var engine = new RazorLightEngineBuilder()
.UseEmbeddedResourcesProject(typeof(Person).Assembly, "ClassLibrary3")
.SetOperatingAssembly(typeof(Person).Assembly)
.UseMemoryCachingProvider()
.Build();
var model = new Person() { Name = "Jean" };
string html = engine.CompileRenderAsync("report", model).Result;
Console.WriteLine(html);
}
}
And now the code of my ClassLibrary .NET 2.0 Standard : My model :
using System;
namespace ClassLibrary3
{
public class Person
{
public string Name { get; set; }
}
}
And my report.cshtml :
@model Person
@using System.Collections.Generic
@using RazorLight
@using ClassLibrary3
@inherits TemplatePage<Person>
Bonjour @Model.Name
And the result is :
TemplateCompilationException: 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?)
- (25:37) The type or namespace name 'TemplatePage<>' could not be found (are you missing a using directive or an assembly reference?)
- (28:66) 'GeneratedTemplate.ExecuteAsync()': no suitable method found to override
- (30:12) The name 'BeginContext' does not exist in the current context
- (31:12) The name 'WriteLiteral' does not exist in the current context
- (32:12) The name 'EndContext' does not exist in the current context
- (33:12) The name 'BeginContext' does not exist in the current context
- (6:3) The name 'Write' does not exist in the current context
- (6:9) The name 'Model' does not exist in the current context
- (39:12) The name 'EndContext' does not exist in the current context
See CompilationErrors for detailed information
Note that :
- I am using :
- RazorLight v2.3.1 nuget package
- Visual Studio 2022 v17.6.2.
- report.cshtml is set as "Embedded resource"
- Same problem, if I add this in csproj of my class library this line :
<PreserveCompilationContext>true</PreserveCompilationContext>
Hi,
I found the solution for my part :
I change this line of code
.SetOperatingAssembly(typeof(Person).Assembly)
by
.SetOperatingAssembly(typeof(Program).Assembly)
And it's solves my problem. Regards Sybaris