adoconnection / RazorEngineCore

.NET6 Razor Template Engine
MIT License
576 stars 85 forks source link

Clode blocks appear in output as plain text #46

Closed kobruleht closed 3 years ago

kobruleht commented 3 years ago

To reproduce, add code block to template like

@{
var test=1;
}

Works as expected. This will appear in result as plain text.

How to run code blocks like in real Razor ?

Using

@if (true) {
var test=1;
}
adoconnection commented 3 years ago
using System;
using RazorEngineCore;

namespace ConsoleApp44
{
    class Program
    {
        static void Main(string[] args)
        {
            RazorEngine engine = new RazorEngine();

            IRazorEngineCompiledTemplate razorEngineCompiledTemplate = engine.Compile(@"
                @if (true) {
                var test=1;
                }
            ");

            Console.WriteLine(razorEngineCompiledTemplate.Run());
            Console.ReadKey();
        }
    }
}

looks ok to me

kobruleht commented 3 years ago

Try without if:

 @{
             var test=1;
                }
adoconnection commented 3 years ago

same output

namespace ConsoleApp44
{
    class Program
    {
        static void Main(string[] args)
        {
            RazorEngine engine = new RazorEngine();

            IRazorEngineCompiledTemplate razorEngineCompiledTemplate = engine.Compile(@"
                 @{
             var test=1;
                }
            ");

            Console.WriteLine("start");
            Console.WriteLine(razorEngineCompiledTemplate.Run());
            Console.WriteLine("end");
            Console.ReadKey();
        }
    }
}

image

kobruleht commented 3 years ago

Please try

<img src='@("test")'>

output is without trailing ' :

<img src='test>
adoconnection commented 3 years ago

could you please submit it a way of console app code, like I do. It will make reproducing a lot easier

kobruleht commented 3 years ago

Code:

using System;
using System.Threading.Tasks;
using RazorEngineCore;

namespace SampleApp
{
    public class Program
    {
        static async Task Main(string[] args)
        {
            RazorEngine razorEngine = new RazorEngine();
            var template =
                await razorEngine.CompileAsync<RazorEngineCorePageModel>(
                    @"<img href='@(""test"")'>");
            var res = await template.RunAsync();
            // Observed: trailing ' is lost
            Console.WriteLine(res);
            Console.ReadKey();
        }
    }
}

Project:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net5.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="RazorEngineCore" Version="2021.3.1" />
    <PackageReference Include="RazorEngineCore.Extensions" Version="0.3.0" />
  </ItemGroup>

</Project>
kobruleht commented 3 years ago

Without RazorEngineCore.Extensions package it works OK .

RazorEngineCore.Extensions provides encoding and Html helper which are used for Html rendering. RazorEngineCore.Extensions seems dead. Why not to add using html encoding by default and Html helper from it to RazorEngineCore.

wdcossey commented 3 years ago

@adoconnection this is resolved on my end, you can close this issue.