ViewBag is not working or I am using it wrong (lack of doco)

Maybe I am not using it correctly, but ViewBag is not working for me:

var razor = HostContext.GetPlugin<RazorFormat>();
var page = razor.GetViewPage("Whatever"); // The page has: @ViewBag.EmailPrefsLink 
var renderingPage = page.ActivateInstance();
renderingPage.ViewBag.EmailPrefsLink = "https://localhost:44301/test-only";
var html = razor.RenderToHtml(page, model); // Resulting html has nothing where @ViewBag.EmailPrefsLink should be.

(model obj passed):

It’s pretty clear that code isn’t going to work, the renderingPage instance is not being used in rendering the page. The razor view instance also isn’t properly initialized which is done behind the scenes and not exposed in public API’s.

The only public API’s provided are rendering the model with the Razor page (with optional layout). Why can’t you put everything you need in the model?

I couldn’t put it in the model and the reason I wanted to use ViewBag is because it is in a partial view (a footer) that is reference from a LAYOUT (well a few different layouts) rather than from a ViewPage itself.

The reason the razor view instance isn’t properly initialized is because I am using servicestack razor as an email templating engine and after I grab the html I am running through premailer.net.

For anyone future ref the way I got around this was to make the ‘EmailPrefsLink’ a property of my CustomServiceStackViewPage…like below:

public abstract class CustomServiceStackViewPage<TModel> : ViewPage<TModel>
{
    public string EmailPrefsLink => $$"ConfigurationManager.AppSettings["EmailCallbackBaseUrl"]}/profile/notifications";
}