Categories
Uncategorized

How to add / import namespace to Razor View in MVC 3

There are two methods in which you can add a namespace to a Razor view in ASP.NET MVC 3.

Method 1
The first way is to use the @using statement in .cshtml files, which imports a namespace to the current file only.

Example :-
[sourcecode language=”csharp”]
@using Namespace1
@using Namespace2.SomeClass
[/sourcecode]

Method 2
and the second way is to define these namespaces in the web.config of the View directory of your project.


Example:-
[sourcecode language=”xml”]
<system.web.webPages.razor>
 <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” />
 .
 <! — can more more here… →
 </namespaces>
 </pages>
</system.web.webPages.razor>
[/sourcecode]

or you can add your custom namespace like this too :
[sourcecode language=”xml”]
<add namespace=”Custom.Yasser” />
<add namespace=”Custom.Mohsin” />
[/sourcecode]