How do I import a namespace in Razor View Page?

In this article, I have explained 2 methods to imports namespace in MVC’s razor view page

Method 1

C#.Net
@using SampleNamespace;
VB.Net
@Imports Mynamespace;

Method 2

Find "web.config" file in "Views" sections of your current project and add following code :

<system.web.webPages.razor>
  <pages pageBaseType="System.Web.Mvc.WebViewPage">
    <namespaces>
      <add namespace="System.Web.Mvc" />
      <add namespace="Samplenamespace.customer" />
<!-- add more namespace as above-->
      .
      .
      
    </namespaces>
  </pages>
</system.web.webPages.razor>

 

Leave a Comment