How do I import a namespace in Razor View Page?

后端 未结 11 1384
迷失自我
迷失自我 2020-11-22 11:03

How to import a namespace in Razor View Page?

相关标签:
11条回答
  • 2020-11-22 11:28

    For Library

    @using MyNamespace
    

    For Model

    @model MyModel
    
    0 讨论(0)
  • 2020-11-22 11:35

    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/

    0 讨论(0)
  • 2020-11-22 11:35

    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

    0 讨论(0)
  • 2020-11-22 11:35

    You can try this

    @using MyNamespace
    
    0 讨论(0)
  • 2020-11-22 11:40

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

    • In first line/s of view add "using your.domainName;" (if it is required in specific view only)
    • if required in all subsequent views then add "using your.domainName;" in _ViewStart.cshtml. You can find more about this in: Where and how is the _ViewStart.cshtml layout file linked?

    • Or add Assembly reference in View web.config as described by others explained in: How do you implement a @using across all Views in Asp.Net MVC 3?

    0 讨论(0)
  • 2020-11-22 11:45

    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;

    0 讨论(0)
提交回复
热议问题