渲染普通剃刀模板时无法进行动态工作

时间:2020-03-18 17:37:04

标签: c# razor dynamic

我有以下简单的C#代码:

using System;
using System.IO;
using RazorHosting;

namespace ConsoleApp4
{
    class Context
    {
        public string Message { get; set; } = "Hello World!";
    }

    class Program
    {
        private const string TEMPLATE = @"
@inherits RazorHosting.RazorTemplateBase
@{
    var message = Context.Message;
    <tag>@message</tag>;
}
";
        static void Main()
        {
            var razor = new RazorEngine<RazorTemplateBase>();
            razor.Configuration.CompileToMemory = false;

            try
            {
                var assemblyName = razor.ParseAndCompileTemplate(null, new StringReader(TEMPLATE));

                string output = null;
                if (assemblyName != null)
                {
                    output = razor.RenderTemplateFromAssembly(assemblyName, new Context());
                }
                if (output == null)
                {
                    Console.WriteLine(razor.ErrorMessage);
                    Console.WriteLine(razor.LastGeneratedCode);
                }
                else
                {
                    Console.WriteLine(output);
                }
            }
            catch (Exception exc)
            {
                Console.WriteLine(exc);
            }

            Console.WriteLine("Press any key to exit");
            Console.ReadKey();
        }
    }
}

csproj文件为:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net472</TargetFramework>
    <OutputType>Exe</OutputType>
  </PropertyGroup>
  <ItemGroup>
    <Reference Include="Microsoft.CSharp" />
    <Reference Include="RazorHosting">
      <HintPath>..\lib\RazorHosting.dll</HintPath>
    </Reference>
    <Reference Include="System.Web.Razor">
      <HintPath>..\lib\System.Web.Razor.dll</HintPath>
    </Reference>
  </ItemGroup>
</Project>

此代码唯一不琐碎的事情是,至少从2011年开始,它依赖于古老的 RazorHosting.dll System.Web.Razor.dll

无论如何,代码及其依赖项位于github上的https://github.com/MarkKharitonov/TestRazor

我的问题是它不起作用-声明为RazorHosting.RazorTemplateBase.Context的属性dynamic的行为类似于普通的object

请注意:

C:\Users\mkharitonov\source\repos\TestRazor [master ≡]> .\TestRazor\bin\Debug\net472\TestRazor.exe
Template Execution Error: 'object' does not contain a definition for 'Message'
//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:4.0.30319.42000
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace @__RazorHost {
    using System;
    using System.Text;
    using System.Collections.Generic;
    using System.Linq;
    using System.IO;


    public class _0d852a7d_8d4d_4472_a337_b511872b9dbe : RazorHosting.RazorTemplateBase {

#line hidden

        public _0d852a7d_8d4d_4472_a337_b511872b9dbe() {
        }

        public override void Execute() {
WriteLiteral("\r\n");


    var message = Context.Message;

WriteLiteral("    <tag>");

    Write(message);

WriteLiteral("</tag>");

                       ;

WriteLiteral("\r\n");

        }
    }
}

Press any key to exit
C:\Users\mkharitonov\source\repos\TestRazor [master ≡]>

现在,我有一个约束-我不能使用最新的Razor引擎,因为我们拥有的实际Razor代码既旧又旧,并且移植起来不容易。当前无法移植到最新的Razor引擎实施中。请不要建议。

那么,鉴于我的简单代码-怎么了?为什么动态不起作用?

0 个答案:

没有答案