I don't normally recommend using ViewBag
, but if you only want to use this to render a title, subclass your controllers from a parent controller say, GeneralController
and set a ViewBag.Title
property there based on domain.
One alternative to this is subclassing any view models from a base view model including similar logic.
public class GeneralController
{
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
base.OnActionExecuting(filterContext);
if(HttpContext != null)
ViewBag.Title = GetRequestPath();
}
private string GetRequestTitle()
{
if(HttpContext.Request.Path.Contains("test.xxx"))
return "Test site";
}
}
Then, any controller and subsequently rendered views will be able to use this ViewBag.Title property. In fact, out of the box, MVC3 includes _Layout.cshtml
as its default layout (or Master Page) that already contains the following line in the head:
<title>@ViewBag.Title</title>