toddams / RazorLight

Template engine based on Microsoft's Razor parsing engine for .NET Core
Apache License 2.0
1.5k stars 259 forks source link

The type or namespace name 'Razor' does not exist in the namespace 'RazorLight' #522

Open gswartz777 opened 1 year ago

gswartz777 commented 1 year ago

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):

gswartz777 commented 1 year ago

Bumping in hopes someone has an idea.

Laantje commented 1 year ago

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:

sybaris commented 1 year ago

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:

See CompilationErrors for detailed information

image

Note that :

sybaris commented 1 year ago

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