By default the new project template for ASP.NET MVC 3 adds the following to the default layout (masterpage in razor):
<title>@ViewBag.Title</title>
The view must then contain the following to assign the title of the page, for instance:
@{
ViewBag.Title = "Log On";
}
Perhaps it is just my own preference but I find that using the ViewBag to hold the title a little bit wrong (I'm thinking too much magic-string flavor). So my question is: Is this the recommended best practice for people using ASP.NET MVC 3 and razor (using a dynamic property bag) or are you opting for something more strongly typed (perhaps involving a custom baseclass?)
I don't think there is any thing bad with default title handling feature that ships with asp.net MVC 3, its okay to do.
I personally do this(below written) to handle title, I am not endorsing the below code or saying that its better than default functionality, its just a preference.
Master
<title>
@RenderSection("Title");
</title>
View
@section Title
{
write title
}
One thing i could suggest to improve default functionality
@{
string pageTitle = @ViewBag.Title ?? "Title Not Set";
}
<title>@pageTitle</title>
So whenever you forget to add it in viewbag, the page will display title= Title Not Set
Creating a base class then making all your controllers inherit from that base-class can also be done. But I think its taking so much of pain for title
.