我试图从使用包
的代码生成gcs<PackageReference Include="Microsoft.AspNetCore.Razor.Language" Version="6.0.5" />
将razor编译成cs的示例代码:
var engine = RazorProjectEngine.Create(RazorConfiguration.Default, RazorProjectFileSystem.Create(pathWhereCsProjLives));
var file = engine.FileSystem.GetItem("/pages/Counter2.razor");
var document = engine.Process(file).GetCSharpDocument();
var generatedCode = document.GeneratedCode;
不能正常工作。例如,将像这样生成一个按钮:
__builder.AddMarkupContent(5, "<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>");
应该是这样的:
__builder.OpenElement(5, "button");
__builder.AddAttribute(6, "class", "btn btn-primary");
__builder.AddAttribute(7, "onclick", Microsoft.AspNetCore.Components.EventCallback.Factory.Create<Microsoft.AspNetCore.Components.Web.MouseEventArgs>(this, IncrementCount
));
__builder.AddContent(8, "Click me");
__builder.CloseElement();
}
我尝试了另一种语法
<button class="btn btn-primary" onclick=@OnClickFunc> ClickMe </button>
生成
__builder.CloseElement();
__builder.AddMarkupContent(5, "n");
__builder.OpenElement(6, "button");
__builder.AddAttribute(7, "class", "btn btn-primary");
__builder.AddAttribute(8, "onclick", OnClickFunc);
__builder.AddContent(9, " ClickMe ");
__builder.CloseElement();
}
private void OnClickFunc() {
Console.WriteLine("OnClick");
}
它在这里工作得很好。
使用<PackageReference Include="Microsoft.AspNetCore.Razor.Language" Version="6.0.13" />