Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

The ultimate page title can be retrieved in ASP.NET MVC by overriding the OnResultExecuting method in the controller and setting the title of the view bag. The view can then access the view bag and display the title in the HTML head section. Here is an example:

public class MyController : Controller
{
    protected override void OnResultExecuting(ResultExecutingContext filterContext)
    {
        // Get the current page's title
        string pageTitle = GetPageTitle();

        // Set the title in the ViewBag
        ViewBag.PageTitle = pageTitle;

        base.OnResultExecuting(filterContext);
    }
}

// In the view, access the title using the ViewBag variable
<head>
    <title>@ViewBag.PageTitle</title>
</head>

Note: GetPageTitle is a custom method that retrieves and formats the title based on the controller and action being executed.