1
可以说我有一个名为MyComponent的ViewComponent。在html字符串中渲染ViewComponent
随着ASP.NET 1.1的核心,我可以通过剃刀视图.cshtml页面内写这使这个ViewComponent:
<vc:my-component></vc:my-component>
我想要做这样的事情:
@{string myHtml = "<vc:my-component></vc:my-component>";}
@(new HtmlString(myHtml))
我安装并试用了RazorEngine,但这样做并不奏效:
string template = "<vc:my-component></vc:my-component>";
var result = Engine.Razor.RunCompile(template, "messageTemplateKey", null, new { Name = "some model data" });
ViewBag.result = result;
然后在.chtml文件中:
@(new HtmlString(ViewBag.result))
背后的故事到这是我创建了一个ViewComponent对如何处理图像文件的一些逻辑,现在我想搜索和替换一些HTML我ViewComponent所有img标签。在我这样做之前,我需要确保它甚至可以工作。
由于有渲染视图为一个字符串的堆栈溢出的例子,是不是努力创造一个视图并将代码放入它中以使用您的视图组件,然后将视图渲染为一个字符串?我很确定这应该适合你。 –