How do I import a namespace in Razor View Page?

using in mvc view
_viewimports.cshtml mvc 5
import in view mvc
razor view error
.net core add namespace
pages pagebasetype
add namespace in view
how to add namespace in web config mvc

How to import a namespace in Razor View Page?

Finally found the answer.

@using MyNamespace

For VB.Net:

@Imports Mynamespace

Take a look at @Javad_Amiry's answer if you want to include a namespace across the app.

Importing Namespaces into Razor Views in ASP.NET MVC 3, The way that namespaces are imported into views is quite different in ASP.NET MVC 3 depending on whether you choose to use the original ASPX view up your web.config and add the namespace under system.web/pages/namespaces: ASP.NET MVC: How to Import namespace in Razor view? Updated on September 20, 2015 Kisan Patel To import a namespace in Razor markup, we use using statement as we write in C# class however it should be prefixed with @. @using MVC.Utility To call any method insider that namespace, we do not need to give a fully qualified name. Assuming LogError

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/

How To Import A Namespace In Razor View Page In Asp.Net MVC , How to add namespaces for Razor pages. at the top of the page. Put all the namespaces in Views\Web.config. the page: Hide Copy Code. In this chapter, we will discuss the Razor View Import. In addition to the ViewStart file, there is also a ViewImports file that the MVC framework will look for when rendering any view. Like the ViewStart file, we can drop ViewImports.cshtml into a folder, and the ViewImports file can influence all

For Library

@using MyNamespace

For Model

@model MyModel

Add namespaces for Razor pages, There are two methods in which you can add a namespace to a Razor view in ASP. The first way is to use the @using statement in .cshtml files, which imports a namespace to the current file only. Example <pages pageBaseType=”System​. Complete steps is described in this video regarding How To Import A Namespace In Razor View Page In Asp.Net MVC If you got any clarification on this, please write it on Comment section. We will

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.

How to add / import namespace to Razor View in MVC 3, In other words, just use the regular Razor syntax for namespace importing. By default ASP.NET Core 1.0 MVC imports the following namespaces  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; this answer answered Sep 7 '11 at 23:25 user933765 Interesting point here! – usefulBee Jan 13 '16 at 19:07

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

Predefined Namespaces And Custom Base View Page in ASP.NET , To import a namespace in Razor view, we use using statement as we write in C# class however it should be prefixed with @. @using MVCTraining5.Utility. Importing namespaces in MVC3 with the Razor View Engine. The new MVC3 Razor view engine has a completely different syntax to the earlier ASPX one and does not use the <%@ style directives that are so familiar to ASP.NET WebForm developers. The good news is that the syntax is much cleaner. To import a namespace into a single Razor view, we use:

Namespace in Razor view in ASP.NET MVC, Next Page. In this chapter, we will discuss the Razor View Import. In addition to the ViewStart file, there is also a ViewImports file that the MVC framework will  To import a namespace in Razor view, we use using statement as we write in C# class however it should be prefixed with @. @using MVCTraining5.Utility . Here we are importing namespace MVCTraining5.Utility namespace. To call any method insider that namespace, we do not need to give a fully qualified name.

ASP.NET Core - Razor View Import, How can i import namespaces in razor page in asp.net mvc. For namespace and Library @using NameSpace_Name. For Model. @model  There are two ways to add namespaces: Put @using namespace at the top of the page. Put all the namespaces in Views\Web.config. For example, if you want to use FunctionName(parameters) in your Razor view from the SiteHelper\BasicHelper class, you can pick any one from the above two ways: #1. Add namespace at the top of the page: @using myMvc3

How do I import a namespace in Razor View Page?, By convention, it is composed from a dot-separated path to the Razor Page location with the name of the application provided as the "root". Having the @namespace declaration in the ViewImports file saves you having to fully qualify the model names in the @model directive of individual pages. You can only have one @namespace directive per

Comments
  • You can also add alias to your imported namespace stackoverflow.com/questions/4798293/mvc3-import-namespace/…
  • Also: They can't go in code blocks. (You'll get a runtime error)
  • Also you don't need the semicolon.
  • This is just bad practice period. Please do not add this to the top of your razor pages. This is messy etc... Correct way is to add to Views - web.config just as @Javad_Amiry points out.
  • It's not bad practice. It's an absolutely necessary feature. web.config is like a global using statement that makes the namespace active in ALL your pages. That may not be what you want if you have classes with the same name in different namespaces. You'll still have a conflict if you try to use them in the same file, but you can resolve that easily within a single file. If you stick it in web.config, then the conflict would arise in all your pages that use either of the classes. So calling this bad practice makes no sense at all.
  • I'm surprised Intellisense doesn't hint to add the using statement the same way it does in normal C# pages.
  • @vtortola : which web.config? the web.config file in Views folder, not the main web.config in root folder. ok?
  • I'd like to hopefully save someone a few minutes of pulling out their hair and say that if you're working within an area, you must add the namespace within the Web.config under /Areas/<AreaName>Views/ rather than /Views/.
  • @MatthijsWessels No it does not need to restart VS. Just build the project and it will take effect. At least I do this always. If a view is open, you have to close that view before build and re-open it after after build.
  • @Javad_Amiry, aha, I did rebuild, but didn't close the view.
  • Yes, this is the correct way. Not the Using statement at top of Razor page like in the OP selected answer....
  • Why is there no ; at the end?
  • @FrenkyB because this isn't C# code, it's Razor code. The using at the beginning of a .cs file is a C# compiler keyword. The @using at the beginning of a .cshtml file is a hint to the Razor template engine.
  • There will be a web.config section in RTM, but we also wanted to provide an API to do this because many users are starting to gravitate away from config. So we have both options available for you!