我知道您可能已经解决了这个问题,但这里有一个适用于母版页和 MVC(以及 ASP.Net 表单)的解决方案。
我首先尝试覆盖母版页的 Render 方法并使用 RenderControl 呈现 ContentPlaceHolders,并将模板中的某些标签替换为呈现的结果。这适用于 ASP.Net 表单,但不适用于 MVC - 这种方式<% using (Html.BeginForm("A","B")) { %>
总是会导致表单标记呈现在页面的最顶部,在文档类型之前。
解决方案
检索模板并将其拆分为多个部分,一些是文本部分,一些是占位符部分。在您的母版页中,您有一个 HTML 文档和您的占位符 - 不仅是您的占位符。这样VS设计师就不会抱怨了。但是,在呈现时,首先清除 Controls 集合,然后将每个部分添加为 LiteralControl 或 ContentPlaceHolder。您只需将实际渲染留给 ASP.Net。下面是灵感代码。
母版页:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title runat="server"></title>
<asp:PlaceHolder ID="HeadPlaceHolder" runat="server">
<script type="text/javascript" src="/cnnet/Resources/Js/jquery-1.8.1.min.js"></script>
</asp:PlaceHolder>
<asp:ContentPlaceHolder ID="HeadContentPlaceHolder" runat="server"/>
</head>
<body>
<asp:ContentPlaceHolder ID="MainContentPlaceHolder" runat="server" />
</body>
</html>
母版页代码隐藏:
private HtmlHead originalPageHeader;
static readonly Regex HeadStartRegex = new Regex(@"^\s*<head[^>]*>");
static readonly Regex HeadEndRegex = new Regex(@"</head>\s*$");
static readonly Regex TitleRegex = new Regex(@"<title>[^<]*</title>");
public Default() { Init += Default_Init; }
private void Default_Init(object sender, EventArgs e) { DoScraping(); }
protected override void Render(HtmlTextWriter writer)
{
// get content from html head control generated via Page.Header:
string headHtml = RenderControl(originalPageHeader);
Controls.Remove(originalPageHeader);
headHtml = HeadStartRegex.Replace(headHtml, string.Empty);
headHtml = HeadEndRegex.Replace(headHtml, string.Empty);
headHtml = TitleRegex.Replace(headHtml, string.Empty);
// head.Controls.Add(new LiteralControl(headHtml)); doesnt work if head content placeholder contains code blocks (i.e. <% ... %>)
// Instead add content this way:
int headIndex = Controls.IndexOf(HeadContentPlaceHolder);
if (headIndex != -1)
Controls.AddAt(headIndex + 1, new LiteralControl(headHtml));
base.Render(writer);
}
private void DoScraping()
{
IList<PagePart> parts = ... // do your scraping and splitting into parts
Controls.Clear();
foreach (PagePart part in parts)
{
var literalPart = part as LiteralPart;
if (literalPart != null)
{
Controls.Add(new LiteralControl(literalPart.Text));
}
else
{
var placeHolderPart = part as PlaceHolderPart;
switch (placeHolderPart.Type)
{
case PlaceHolderType.Title:
Controls.Add(new LiteralControl(HttpUtility.HtmlEncode(Page.Title)));
break;
case PlaceHolderType.Head:
Controls.Add(HeadPlaceHolder);
Controls.Add(HeadContentPlaceHolder);
break;
case PlaceHolderType.Main:
Controls.Add(new LiteralControl("<div class='boxContent'>"));
Controls.Add(MainContentPlaceHolder);
Controls.Add(new LiteralControl("<div/>"));
break;
}
}
}
}
private string RenderControl(Control control)
{
string innerHtml;
using (var stringWriter = new StringWriter())
{
using (var writer = new HtmlTextWriter(stringWriter))
{
control.RenderControl(writer);
writer.Flush();
innerHtml = stringWriter.ToString();
}
}
return innerHtml;
}
部分:
public class PagePart {}
public class LiteralPart : PagePart
{
public LiteralPart(string text) { Text = text; }
public string Text { get; private set; }
}
public class PlaceHolderPart : PagePart
{
public PlaceHolderPart(PlaceHolderType type) { Type = type; }
public PlaceHolderType Type { get; private set; }
}
public enum PlaceHolderType { Title, Head, Main }
对于拆分:
class PlaceHolderInfo
{
public PlaceHolderInfo(PlaceHolderType type, Regex splitter)
{
Type = type;
Splitter = splitter;
}
public PlaceHolderType Type { get; private set; }
public Regex Splitter { get; private set; }
}
private static readonly List<PlaceHolderInfo> PlaceHolderInfos = new List<PlaceHolderInfo>
{
new PlaceHolderInfo(PlaceHolderType.Title, new Regex(TitleString)),
new PlaceHolderInfo(PlaceHolderType.Head, new Regex(HeadString)),
new PlaceHolderInfo(PlaceHolderType.Main, new Regex(MainString)),
};
private static List<PagePart> SplitPage(string html)
{
var parts = new List<PagePart>(new PagePart[] { new LiteralPart(html) });
foreach (PlaceHolderInfo info in placeHolderInfos)
{
var newParts = new List<PagePart>();
foreach (PagePart part in parts)
{
if (part is PlaceHolderPart)
{
newParts.Add(part);
}
else
{
var literalPart = (LiteralPart)part;
// Note about Regex.Split: if match is found in beginning or end of string, an empty string is returned in corresponding end of returned array.
string[] split = info.Splitter.Split(literalPart.Text);
for (int i = 0; i < split.Length; i++)
{
newParts.Add(new LiteralPart(split[i]));
if (i + 1 < split.Length) // If result of Split returned more than one string, it means there was a match and we insert the placeholder between each string
newParts.Add(new PlaceHolderPart(info.Type));
}
}
}
parts = newParts;
}
return parts;
}
请注意,此解决方案很容易扩展到更多占位符(面包屑、菜单,您可以命名)。它不对模板中占位符的顺序或它们的存在做任何假设。
编辑1:我最初DoScraping
是从Render
方法中调用的。事实证明这是有问题的,因为它确实对 Web 表单中的控件名称进行了重新编号(例如 ctl00$MainContentPlaceHolder$RequestingRepeater$ctl01$ctl01)。它把数字弄乱了,以至于OnCommand
中继器内的按钮停止工作。必须尽早对控件进行重新排序以避免这种情况,因此已将其移至Init
现在。
编辑 2:某些页面用于Page.Header
生成样式和脚本标签。为了支持这个功能,我添加了一些技巧来保留原始<head>
标签并在渲染时插入生成的内容。