在ASP.NET Web Pages / Razor中有一种方法可以包含一个文件,比如Helper或类似的东西,而不知道它的名字?
在Web窗体中,我通过动态添加用户控件来执行此操作,如下面的代码,但我想知道是否有一种在Web页面中执行类似操作的方法?
ContentTemplateBase control = (ContentTemplateBase)Page.LoadControl("~/ContentTemplates/" + detail.Template + ".ascx"); control.DataSource = detail; plhTemplate.Controls.Add(control);我知道我可以通过一堆条件语句来做到这一点,比如......
if (detail.Template== "Template1") { @MyHelpers.Template1(detail) } else if (detail.Template== "Template2") { @MyHelpers.Template2(detail) } ... etc...但希望这样做而不必列出所有可能的选项。
In ASP.NET Web Pages/Razor is there a way to include a file, such as a Helper or something similar, without knowing it's name?
In Web Forms I do this by dynamically adding a User Control, like the code below, but am wondering if there is a way of doing something similar in Web Pages?
ContentTemplateBase control = (ContentTemplateBase)Page.LoadControl("~/ContentTemplates/" + detail.Template + ".ascx"); control.DataSource = detail; plhTemplate.Controls.Add(control);I know I could do this with a bunch of conditional statements, like...
if (detail.Template== "Template1") { @MyHelpers.Template1(detail) } else if (detail.Template== "Template2") { @MyHelpers.Template2(detail) } ... etc...but wanting to do this without having to list all possible options.
最满意答案
在重新阅读Web页面文档时,我看到我可以使用RenderPage ()方法来包含来自另一个文件的代码,所以这似乎是最好的选择。
On re-reading the Web Pages documentation I see I can use the RenderPage() method to include code from another file, so this seems to be the best option.
更多推荐
发布评论