sitefinitysitefinity-13

How to set title programmatically in sitefinity mvc


Can you please help with setting the title as page name not data from the widget in Sitefinity? I have tried

var urlName = SiteMapBase.GetActualCurrentNode().UrlName;
var pageHandler = this.ViewContext.HttpContext.Handler.GetPageHandler();
pageHandler.Title = urlName.ToUpperInvariant();
pageHandler.Page.Title = "MyTitle";

But it doesn't work. A version of Sitefinity 13.1.


Solution

  • At what stage of the page lifecycle are you setting it?

    Most probably Sitefinity rewrites this after your code.

    Your best bet is to place your code on PreRenderComplete event.

    So, you know how you found the pageHandler - try subscribing to its PreRenderComplete event and in there set the Title of the page.