How do I import a namespace in Razor View Page?

asp.netasp.net Mvc-3RazorWebmatrix

asp.net Problem Overview


How to import a namespace in Razor View Page?

asp.net Solutions


Solution 1 - asp.net

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.

Solution 2 - asp.net

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:

<system.web.webPages.razor>
  <pages pageBaseType="System.Web.Mvc.WebViewPage">
    <namespaces>
      <add namespace="System.Web.Mvc" />
      <add namespace="System.Web.Mvc.Ajax" />
      .
      .
      <!-- etc -->
    </namespaces>
  </pages>
</system.web.webPages.razor>

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">

Regards.


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 /Views/

Solution 3 - asp.net

For Library

@using MyNamespace

For Model

@model MyModel

Solution 4 - asp.net

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

Microsoft.WebPages.Compilation.CodeGeneratorSettings.AddGlobalImport("Namespace.Namespace");

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

Solution 5 - asp.net

I found this http://weblogs.asp.net/mikaelsoderstrom/archive/2010/07/30/add-namespaces-with-razor.aspx 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()
   {
       CodeGeneratorSettings.AddGlobalImport("Custom.Namespace");
   }
}

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

Solution 6 - asp.net

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;

Solution 7 - asp.net

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)
   {  
          <tr>
                <td>@item.srno</td>
                <td>@item.name</td>
         </tr>  
   }
       

Solution 8 - asp.net

You can try this

@using MyNamespace

Solution 9 - asp.net

"using MyNamespace" works in MVC3 RTM. Hope this helps.

Solution 10 - asp.net

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

@using XX.YY.ZZ

Solution 11 - asp.net

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAmitabhView Question on Stackoverflow
Solution 1 - asp.netAmitabhView Answer on Stackoverflow
Solution 2 - asp.netamiry jdView Answer on Stackoverflow
Solution 3 - asp.netAlper ŞaldırakView Answer on Stackoverflow
Solution 4 - asp.netGermánView Answer on Stackoverflow
Solution 5 - asp.netk-devView Answer on Stackoverflow
Solution 6 - asp.netuser933765View Answer on Stackoverflow
Solution 7 - asp.netMahaveer JangidView Answer on Stackoverflow
Solution 8 - asp.netAbhishek SiddhuView Answer on Stackoverflow
Solution 9 - asp.netHowardView Answer on Stackoverflow
Solution 10 - asp.netHiteshAjudiyaView Answer on Stackoverflow
Solution 11 - asp.netImran JavedView Answer on Stackoverflow