How do I import a namespace in Razor View Page?

How to import a namespace in Razor View Page?


Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Finally found the answer.

@using MyNamespace

For VB.Net:

@Imports Mynamespace

Take a look at @ravy amiry’s answer if you want to include a namespace across the app.

Method 2

The first way is that use @using statement in .cshtml files, that imports a namespace to current file only, and the second:

In the “web.config” file in “Views” directory of your project (notice it is not the main web.config in project’s root), find this section:

  <pages pageBaseType="System.Web.Mvc.WebViewPage">
      <add namespace="System.Web.Mvc" />
      <add namespace="System.Web.Mvc.Ajax" />
      <!-- etc -->

you can add your custom namespace like this:

<add namespace="My.Custom" />

that will add the namespace to all of .cshtml (and/or .vbhtml) files;
also you can change views inheritance from here, like:

<pages pageBaseType="My.Custom.MyWebViewPage">


UPDATE: Thanks to @Nick Silberstein to his reminder about areas! He said:

If you’re working within an area, you must add the namespace within the Web.config under /Areas/<AreaName>/Views/ rather than

Method 3

For Library

@using MyNamespace

For Model

@model MyModel

Method 4

In ASP.NET MVC 3 Preview1 you can import a namespace on all your razor views with this code in Global.asax.cs


I hope in RTM this gets done through Web.config section.

Method 5

I found this which explains how to add a custom namespace to all your razor pages.

Basically you can make this

using Microsoft.WebPages.Compilation;
public class PreApplicationStart
   public static void InitializeApplication()

and put the following code in your AssemblyInfo.cs

[assembly: PreApplicationStartMethod(typeof(PreApplicationStart), "InitializeApplication")]

the method InitializeApplication will be executed before Application_Start in global.asax

Method 6

One issue that you must know is that when you import a namespace via web.config in Views folder, that namespace is imported JUST for views in that folder. Means if you want to import a namespace in an area views, you must also import that namespace, in that area’s web.config file, located in area’s Views folder;

Method 7

For namespace and Library

@using NameSpace_Name

For Model

@model Application_Name.Models.Model_Name

For Iterate the list on Razor Page (You Have to use foreach loop for access the list items)

@model List<Application_Name.Models.Model_Name>

@foreach (var item in Model)

Method 8

You can try this

@using MyNamespace

Method 9

“using MyNamespace” works in MVC3 RTM. Hope this helps.

Method 10

I think in order import namespace in razor view, you just need to add below way:

@using XX.YY.ZZ

Method 11

Depending on your need you can use one of following method:

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x