How to set title programmatically in sitefinity mvc

263 Views Asked by At

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.

3

There are 3 best solutions below

0
Veselin Vasilev On BEST ANSWER

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.

0
Steve McNiven-Scott On

Do you just simply mean

ViewBag.Title = "My title";

In a controller action?

0
user_vgizy On

This works 12.2 but could not try 13.1

SiteMapBase.GetActualCurrentNode().Title