As of Beta 5 of ASP.NET 5, there is a new file in the Views folder of an MVC project called _ViewImports.cshtml (here's the GitHub item). In previous Beta versions, this file was called _GlobalImport.cshtml; even though it has been renamed, its functionality is the same in Beta 5 as in Beta 3. What exactly does it do? Let's find out!


_ViewImports.cshtml serves one major purpose: to provide namespaces which can be used by all other views. In previous MVC projects, this functionality was provided by the web.config file in the Views folder; since the web.config no longer exists, global namespaces are now provided by this file.

At any rate, a simple _ViewImports.cshtml file might look like this:

@using MyProject.Web
@using Microsoft.Framework.OptionsModel
@addTagHelper "*, Microsoft.AspNet.Mvc.TagHelpers"

The first two lines are fairly easy to understand: we want the MyProject.Web and Microsoft.Framework.OptionsModel namespaces to be available to all views. If we wanted another namespace to be available, we'd simply add it here.

The last line is a bit trickier. We've already seen examples of TagHelpers in previous posts but we've glossed over exactly how they get enabled; I want to remedy that in this post.

The reason that we use the @addTagHelper keyword is because we need the Tag Helpers to actually execute (at some point in the page rendering lifecycle; I'm not sure exactly where yet) rather than just be available to the views. So, we specify a tag helper namespace, and the asterisk (*) specifies that we want all tag helpers in that namespace to be used.

That's it! The _ViewImports.cshtml file is responsible for setting up namespaces that can be accessed by the views in your MVC 6 project, which was previously done by the web.config file in the views folder. IMO this solution is much easier to read, but you should go make up your own mind. Now go out there and start using ASP.NET 5 and MVC 6; trust me, you'll be glad you did.

Happy Coding!