Add namespace to all views in ASP.NET MVC 6
Solution 1
For <= beta3 bits (what you're most likely using) you should add an @using
statements to your _ViewStart.cshtml. Aka:
_ViewStart.cshtml: @using MyProject.WebUI.Helpers
If you don't have a _ViewStart.cshtml you can create one and just make sure it's in the same path or parent path of the view you want it to affect.
For beta4 bits, this functionality was moved to a new file called _GlobalImport.cshtml; _ViewStart.cshtml was transitioned back to its original functionality (just running code, not inheriting directives). Therefore:
_GlobalImport.cshtml: @using MyProject.WebUI.Helpers
For beta5 bits, _GlobalImport.cshtml was renamed to _ViewImports.cshtml
Solution 2
Add your namespaces to the_ViewImports.cshtml
file (it's under the Views folder).
Example file:
@using Microsoft.AspNetCore.Identity
@using Jifiti.Registry.Web.Models.AccountViewModels
@using Jifiti.Registry.Web.Models.ManageViewModels
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
Related videos on Youtube
Dylan Parry
Web developer living in Gloucester, UK. Currently working with C#, ASP.NET MVC, HTML, CSS, JavaScript.
Updated on September 15, 2022Comments
-
Dylan Parry over 1 year
I’m using MVC 6 and would like to be able to access a particular namespace globally from all of my Razor views. In MVC 5 this was fairly simple; I’d just add the following code to my
~/views/web.config
file:<system.web.webPages.razor> <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.1.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /> <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" /> <add namespace="MyProject.WebUI" /> <add namespace="MyProject.WebUI.Helpers" /><!-- Added this line --> </namespaces> </pages> </system.web.webPages.razor>
Where I’ve added access to the
MyProject.WebUI.Helpers
namespace.In ASP.NET 5, and therefore MVC 6, the
web.config
file has be done away with, so I’m not sure how to go about doing this any more. I’ve tried searching for an answer, but all I can find is how to do it in current versions of ASP.NET rather than v5.Any ideas?
Edit: Clarified which
web.config
file I would have used.-
Frebin Francis about 9 yearswhere did you add this namespace ~/web.config or ~/views/web.config ?
-
Dylan Parry about 9 yearsThis would have been in
~/views/web.config
, but ASP.NET 5 doesn’t useweb.config
files at all, so I have no idea what I need to add or even where I need to add it to. -
Frebin Francis about 9 yearsIf you add these files on ~/views/web.config file please unload your project and the try reload after that.
-
Dylan Parry about 9 yearsVisual Studio 2015 (CTP 6) doesn’t seem to even have the option to add a
web.config
file. I’m presuming that I’d need to use aConfig.json
file, but I’m not sure what the syntax is for adding a namespace with that. -
Frebin Francis about 9 yearsNo ~/views/web.config in MVC6 application ? is config.json is there in ~/views/ folder ?
-
Dylan Parry about 9 yearsNope, neither of the files exist. I’m presuming that MVC 6 is hardwired with a lot of the settings that would have originally been in the
~/views/web.config
as I can’t see anything in my project that’s setting these things, and the rootconfig.json
currently only has stuff for Entity Framework and ASP.NET Identity 3.0.
-
-
Bart Calixto almost 9 yearsin Beta5 is
_ViewImports.cshtml
github.com/aspnet/Announcements/issues/27 -
WillC about 8 yearsSeems redundant to have both _ViewStart and _ViewImports to me.
-
gdoron is supporting Monica almost 8 yearsIf you don't mind, I added an answer with just the latest bits, it's redundant to specify beta 3,4,5 when everyone now is using RC1+ and RTM is very close.
-
Georgi Kovachev over 7 yearsI don't have Views folder nor _ViewImports.cshtml
-
Cemal about 3 yearsI think a bit more explanation on which file these go, or when to use these directives, would make a better answer.