Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at

  • Stephen 6 posts 96 karma points
    Nov 06, 2019 @ 12:57

    Umbraco 8 - getting umbraco-based information into WebForms legacy page

    Hi folks

    I'm building a new Umbraco 8 site based on a much older WebForms site. Most of the site is being rebuilt in MVC with proper Umbraco integration, but there are a couple of legacy pages which, for the moment, need to stay as WebForms pages - we just do not have the resources to rebuild them. I've been asked to see if we can incorporate the Umbraco-based Header and Footer views into the WebForms aspx pages in some fashion.

    I just need to be able to pull a few facets of the HomePage ContentModel in - a list of its Children to construct the same set of top links as is in the MVC pages, and one of the properties of its IPublishedContent so that I can get the URL of a banner image. Is there any way at all that I can access Umbraco.ContentAtRoot() or the general UmbracoViewPage structure from within an aspx context? Can I get the information directly from Umbraco's SQL database tables?


  • Stephen 6 posts 96 karma points
    Nov 13, 2019 @ 13:43

    I eventually solved this one myself with help from Google.

    For anyone else stuck with the same issue, if you include

    using Umbraco.Web.Composing;

    you can then access


    to make use of the homepage. I was then able to use a slightly modified version of Keith's answer from, melded with aarondcoleman's answer, to render the partial view as a string that I could then include in the WebForms page.

    public static string RenderPartial(string partialName, object model)
            //get a wrapper for the legacy WebForm context
            var httpCtx = new HttpContextWrapper(HttpContext.Current);
            //create a mock route that points to the empty controller
            var rt = new RouteData();
            rt.Values.Add("controller", "WebFormController");
            //create a controller context for the route and http context
            var ctx = new ControllerContext(new RequestContext(httpCtx, rt), new WebFormController());
            //find the partial view using the viewengine
            var view = ViewEngines.Engines.FindPartialView(ctx, partialName).View;
            //create a view context and assign the model
            var vctx = new ViewContext(ctx, view, new ViewDataDictionary { Model = model }, new TempDataDictionary(), new StringWriter());
            // This will render the partial view direct to the output, but be careful as it may end up outside of the <html /> tag
            //view.Render(vctx, HttpContext.Current.Response.Output);
            // Better to render like this and create a literal control to add to the parent
            var html = new StringWriter();
            view.Render(vctx, html);
            return html.GetStringBuilder().ToString();

    and then in the code-behind Page_Load function,

    HeaderContent = WebFormMVCUtil.RenderPartial("~/Views/Partials/WebFormsCustomHeader.cshtml", Current.UmbracoHelper.ContentAtRoot().FirstOrDefault());

    (HeaderContent being declared as a public string in the partial class of the page)

    (Incidental note, when I put

    <%= HeaderContent %>

    into the code of the aspx page, I initially got an error from Visual Studio that it couldn't find HeaderContent, but the error vanished once I compiled and actually ran the page, so I think that was just Visual Studio being weird.)

Please Sign in or register to post replies

Write your reply to: