【发布时间】:2017-04-05 08:25:58
【问题描述】:
假设我有一个名为 MyComponent 的 ViewComponent。
从 ASP.NET Core 1.1 开始,我可以通过在 razor 视图 .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,其中包含一些关于如何处理图像文件的逻辑,现在我想用我的 ViewComponent 搜索和替换某些 html 中的所有 img 标签。在我这样做之前,我需要确保它甚至可以工作。
【问题讨论】:
-
由于存在在堆栈溢出时将视图渲染为字符串的示例,因此创建视图并将代码放入其中以使用您的视图组件然后将视图渲染到细绳?我很确定这对你有用。
标签: c# asp.net-mvc asp.net-core asp.net-core-viewcomponent