C1 正在使用 Webforms 来托管其内容,因此您现在在 Webforms 项目中没有什么是您不能做的。您可以将Webforms Masterpages用作内容的模板,并且完全支持路由,可以根据需要添加任意数量的自定义路由,或者只是覆盖默认的 c1 页面路由。或者依赖PathInfo Api,其中 url 的不匹配部分将可用于与 url 的最长部分匹配的页面上。如果您喜欢 Microsoft 的新语法,也可以在 C1 中使用Razor (.cshtml) 。
您也可以扭转局面,选择在您自己现有的页面上呈现来自 C1 的内容。此处的代码显示了如何使用标准 WebForm .aspx 页面在 C1 中呈现来自特定页面的内容
public class BaseContentTeaserPage : Page
{
private Guid _pageId;
protected PlaceHolder plc;
protected override void OnPreInit(EventArgs e)
{
_pageId = Guid.Parse(Request.QueryString["pageId"]);
using (new DataScope(PublicationScope.Published, new CultureInfo("en-GB")))
{
using (var data = new DataConnection())
{
PageRenderer.CurrentPage = data.Get<IPage>().Single(p => p.Id == _pageId);
var urlData = new PageUrlData(PageRenderer.CurrentPage);
urlData.PathInfo = Request.QueryString["pathInfo"];
Context.Items["C1_PageUrl"] = urlData;
}
}
base.OnPreInit(e);
}
protected override void OnLoad(EventArgs e)
{
using (new DataScope(PublicationScope.Published, new CultureInfo("en-GB")))
{
var content = DataFacade.GetData<IPagePlaceholderContent>().Single(p => p.PageId == _pageId);
var helper = new PageRendererHelper();
var mapper = (IXElementToControlMapper)helper.FunctionContext.XEmbedableMapper;
var doc = helper.RenderDocument(content);
var body = PageRendererHelper.GetDocumentPart(doc, "body");
addNodesAsControls(body.Nodes(), plc, mapper);
if (Page.Header != null)
{
var head = PageRendererHelper.GetDocumentPart(doc, "head");
if (head != null)
{
addNodesAsControls(head.Nodes(), Page.Header, mapper);
}
}
}
base.OnLoad(e);
}
private static void addNodesAsControls(IEnumerable<XNode> nodes, Control parent, IXElementToControlMapper mapper)
{
foreach (var node in nodes)
{
var c = node.AsAspNetControl(mapper);
parent.Controls.Add(c);
}
}
protected override void Render(HtmlTextWriter writer)
{
var markupBuilder = new StringBuilder();
using (var sw = new StringWriter(markupBuilder))
{
base.Render(new HtmlTextWriter(sw));
}
string xhtml = markupBuilder.ToString();
using (Profiler.Measure("Changing 'internal' page urls to 'public'"))
{
xhtml = PageUrlHelper.ChangeRenderingPageUrlsToPublic(xhtml);
}
using (Profiler.Measure("Changing 'internal' media urls to 'public'"))
{
xhtml = MediaUrlHelper.ChangeInternalMediaUrlsToPublic(xhtml);
}
writer.Write(xhtml);
}
}