What's the most practical way to add custom properties to a side wide ViewDataDictionary?
Let's assume a simple case:
public ActionResult Index()
{
// ViewData["Title"] = "Home";
ViewData.Title = "Home";
return View();
}
The first thing that comes to mind is a custom class and using "new" in a application base controller:
public class CustomDataDictionary : ViewDataDictionary
{
public string Title { get; set; }
}
public class ApplicationController : Controller
{
public new CustomDataDictionary ViewData { get; set;}
}
I'm assuming you would also have to also make a corresponding CustomViewPage with the same property to use the custom view data dictionary in the views as well.
The second thing that comes to mind is to create a ViewDataDictionaryExtensions class.
Thirdly, use a "View Model". My beef with View Models are that always creating one and passing it into View seems like repeating yourself over and over in controller code, at least compared to the previous two options.
The real goal is that each application might have a core set of properties in the ViewData that make sense for that apps purpose. I tend to shy away from relying on dictionary keys, and it would be nice to have real properties for other developers to rely one to know what data to set. Plus, keys can easily be misspelled.
All three methods get the job done. What have others done?