Umbraco Render View to String

Find below a helpful controller extension class that allows you to just call 'RenderViewAsync' from any controller type and it'll render the contents of the view to a string for use anywhere:

public static async Task RenderViewAsync(this ControllerBase objController, String strViewName, TModel objModel, Boolean blnPartial = false)
{
    if (String.IsNullOrEmpty(strViewName))
    {
        strViewName = objController.ControllerContext.ActionDescriptor.ActionName;
    }

    ViewDataDictionary colViewDictionary = new ViewDataDictionary(new EmptyModelMetadataProvider(), new ModelStateDictionary())
    { 
        Model = objModel
    };

    using (StringWriter objStringWriter = new StringWriter())
    {
        IViewEngine? objViewEngine = objController.HttpContext.RequestServices.GetService(typeof(ICompositeViewEngine)) as ICompositeViewEngine;
        if (objViewEngine == null)
        {
            return String.Empty;
        }

        ViewEngineResult objViewResult = objViewEngine.FindView(objController.ControllerContext, strViewName, !blnPartial);
        if (objViewResult.Success == false)
        {
            return $"A view with the name {strViewName} could not be found";
        }

        ITempDataProvider? objTempDataProvider = objController.HttpContext.RequestServices.GetService(typeof(ITempDataProvider)) as ITempDataProvider;
        if (objTempDataProvider == null)
        {
            return String.Empty;
        }

        ViewContext objViewContext = new ViewContext(
            objController.ControllerContext,
            objViewResult.View,
            colViewDictionary,
            new TempDataDictionary(objController.HttpContext, objTempDataProvider),
            objStringWriter,
            new HtmlHelperOptions()
        );

        await objViewResult.View.RenderAsync(objViewContext);

        return objStringWriter.GetStringBuilder().ToString();
    }
}

Notice the use of services via the HttpContext RequestServices in order to get the ICompositeViewEngine and ITempDataProvider.


Published at

Tags: Umbraco,.net 8.0,C#

Luke Alderton

Comments

Share with
Tags
Latest Comments
By Mark Gentry on Windows Server 2019 - Change product key does nothing
20 Aug 2021, 03:30 AM
By Cathy on In-Place Upgrade for a Windows Server domain controller
31 Jul 2021, 18:28 PM
By Mr. Greymatter on Raspberry Pi - Running Java app on Raspbian
16 Feb 2021, 07:35 AM
By Mikko Seittenranta on Xamarin Forms multiple instances of same app open
16 Feb 2021, 04:34 AM
By Andrew on Auto/Custom height on Xamarin Forms WebView for Android and iOS
22 Jan 2021, 22:15 PM
By Nick on Raspberry Pi - Running Java app on Raspbian
14 Oct 2020, 19:37 PM
By Ivan on Fixed: Value cannot be null Parameter Name: source
15 Sep 2020, 19:47 PM
By Anand on Raspberry Pi - Bluetooth using Bluecove on Raspbian
7 Sep 2020, 16:53 PM
Categories
App Development
Event
Game Development
Mapping
Modelling
Programming
Review
Robotics
Tutorial
Web Development