Why can't _ViewStart.cshtml access the ViewBag object?

Viewed 17719

I have the default _ViewStart.cshtml in my /Views folder. I'd like to be able to access my ViewBag object so I can set the default title for all my views.

However, with:

@{
    Layout = "~/Views/Shared/SiteLayout.cshtml";
    ViewBag.Title = "bytecourse - Online Courses in Technology";
}

I get "The name 'ViewBag' does not exist in the current context" as a runtime error.

What do I need to do?

6 Answers

You can create one layout page which is using your Viewbag data and add layout to your blank _ViewStart page it will work

on ViewStart.cshtml

@{
    Layout = "~/Views/Shared/_Layout.cshtml";
}
Related