【问题标题】:Intellisense in Razor with Nancy与 Nancy 一起在 Razor 中进行智能感知
【发布时间】:2014-04-28 03:50:01
【问题描述】:

我使用 Visual Studio 2013 在 .Net 4.5.1 中启动了一个新的 Nancy 项目。 作为视图引擎,我使用的是 Razor。 一切都可以构建和工作,但我希望智能感知不使用完整的命名空间。 这可能吗?

这是我的 web.config:

<configuration>
  <configSections>
    <section name="razor" type="Nancy.ViewEngines.Razor.RazorConfigurationSection, Nancy.ViewEngines.Razor" />
    <sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
      <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false"/>
    </sectionGroup>
  </configSections>

  <razor disableAutoIncludeModelNamespace="false">
    <assemblies>
      <add assembly="IC.Resources" />
    </assemblies>
    <namespaces>
      <add namespace="IC.Web.Client.Models" />
      <add namespace="IC.Resources" />
    </namespaces>
  </razor>

  <system.web.webPages.razor>
    <pages pageBaseType="Nancy.ViewEngines.Razor.NancyRazorViewBase">
      <namespaces>
        <add namespace="Nancy.ViewEngines.Razor"/>
        <add namespace="IC.Web.Client.Models" />
        <add namespace="IC.Resources" />
      </namespaces>
    </pages>
  </system.web.webPages.razor>
</configuration>

如果我使用来自 IC.Resources 或 IC.Web.Client.Models 的代码,我仍然需要使用完整的命名空间。

这是我的观点:

@inherits Nancy.ViewEngines.Razor.NancyRazorViewBase<IC.Web.Client.Models.LoginModel>

如果我将它添加到每个页面的顶部,它会起作用:

@using IC.Resources;
@using IC.Web.Client.Models

但我不喜欢在每一页上都重复这一点。 我尝试将 using 仅添加到布局页面,但这不起作用。

@{ Layout = "_layout.cshtml"; }

【问题讨论】:

    标签: c# .net razor nancy


    【解决方案1】:

    如果您将 System.Object 定义添加到您的 pageBaseType 中,则应该不需要添加两个 using 语句。

    <system.web.webPages.razor>
        <pages pageBaseType="Nancy.ViewEngines.Razor.NancyRazorViewBase`1[[System.Object]]">
            <namespaces>
                <add namespace="Nancy.ViewEngines.Razor" />
            </namespaces>
        </pages>
    </system.web.webPages.razor>
    

    【讨论】:

    • 真的很有帮助!但是你能解释一下'`1[[System.Object]]'语句吗?
    • 这是 .net 中某处的类型定义。当我在通用实例上使用 p.GetType().Name 之前,我已经看到了这一点。原始定义是“Nancy.ViewEngines.Razor.NancyRazorViewBase”。不要问我为什么会这样。
    猜你喜欢
    • 1970-01-01
    • 2015-02-25
    • 2015-01-07
    • 2011-07-07
    • 2016-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多