我正在将一些 MVC 页面添加到已经存在的 WebForms 项目中。我想继续对 WebForms 母版页/辅助方法进行尽可能少的更改。WebForms 项目使用 asp:Menu 构建菜单导航,因此我创建了一个 HTML 帮助器方法,该方法动态构建相同的菜单,将控件呈现给包装 StringBuilder 的 HtmlWriter,然后返回输出。
这是我的代码:
<Extension()> _
Public Function RenderMenu(helper As HtmlHelper) As MvcHtmlString
'Build menu with asp .net standard menu control
Dim menuSource As New System.Web.UI.WebControls.SiteMapDataSource() With {
.ShowStartingNode = False,
.SiteMapProvider = "MySiteMapProvider"
}
Dim menuBuilder As New System.Web.UI.WebControls.Menu() With {
.CssClass = "menu",
.IncludeStyleBlock = "false",
.Orientation = System.Web.UI.WebControls.Orientation.Horizontal,
.StaticSubMenuIndent = New System.Web.UI.WebControls.Unit(20),
.RenderingMode = System.Web.UI.WebControls.MenuRenderingMode.List,
.DataSource = menuSource
}
menuBuilder.DynamicHoverStyle.BorderStyle = System.Web.UI.WebControls.BorderStyle.Dotted
Dim buffer As New StringBuilder()
Dim writer As New System.Web.UI.HtmlTextWriter(New StringWriter(buffer))
menuBuilder.RenderControl(writer)
'Convert rendered html to an mvc html string (to avoid re-encoding)
Return New MvcHtmlString(buffer.ToString())
End Function
问题是,我的 HtmlWriter 没有呈现任何内容!我已经在调试器上逐步完成并确认我的 menuSource 有数据。我也尝试过手动将项目添加到菜单中,但结果相同。
这可能是菜单控件的 Page 属性未设置的某种问题吗?如果是这样,有没有办法伪造它?