How do I import a namespace in Razor View Page?
如何在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.
 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/  
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.
链接地址: http://www.djcxy.com/p/65504.html上一篇: 具有请求和响应DTO的服务栈剃刀视图
