I think you're asking why you have to add usings for your classes, but other Microsoft classes and such are available in the view automatically without needing usings? There's a bit of black magic you're not seeing. The good news is that you can do the same thing. If you expand the Views
directory in your project, you should see a Web.config file in there. This is different from the main Web.config used for your project; this one applies just to things in the Views
directory. If you open this file, you'll see a section inside like:
<system.web.webPages.razor>
<host factoryType="..." />
<pages pageBaseType="System.Web.Mvc.WebViewPage">
<namespaces>
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Optimization"/>
<add namespace="System.Web.Routing" />
...
</namespaces>
</pages>
</system.web.webPages.razor>
Behold: the missing usings. In case you haven't guessed, you can add your own namespaces here. Anything you add here will have the effect of automatically adding a using statement to your view for that namespace.
UPDATE
Whoops. Just noticed the core tag. I don't think this works anymore with core, but I'll leave my answer just in case someone needs the MVC method. In ASP.NET Core, you'll create a _ViewImports.cshtml
file in your Views
directory, instead, and add your using statements there:
_ViewImports.cshtml
@using MyProject.Models