3

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

Mr. Bungle
  • 53
  • 5

3 Answers3

6

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>

Sparkle
  • 2,459
  • 3
  • 18
  • 20
0

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:

https://stackoverflow.com/a/6723046/645167

Community
  • 1
  • 1
amiry jd
  • 27,021
  • 30
  • 116
  • 215
0

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" />
Huske
  • 9,186
  • 2
  • 36
  • 53
  • like [Eonasdan said in the comment](http://stackoverflow.com/posts/7018805/edit) you should put this in the *Views* `web.config` – drzaus Apr 02 '14 at 19:31