TO import a namespace, I know how to use the @using MyNamespace. Does anyone know how to globally import a namespace in all razor views?
I heard of a AddGlobalImport method, not sure how to use it though
TO import a namespace, I know how to use the @using MyNamespace. Does anyone know how to globally import a namespace in all razor views?
I heard of a AddGlobalImport method, not sure how to use it though
This link explains in detail how to achieve this.
http://www.c-sharpcorner.com/UploadFile/jaishmathews/8652/
Simple add the namespace to your web config:
<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=3.0.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.Routing" />
<add namespace="MvcApplication1.Utilities"/>
</namespaces>
</pages>
As @Sparkle said, you can use the web.config
file under ~/Views/ folder and/or ~/Areas/AreaName/Views/. But, here is a total solution describes multiple situations:
Add the namespace in web.config under namespaces
element. You already have some namespaces defined there. It is under system.web/pages/namespaces
.
<add namespace="My.Custom.Namespace" />