asp.net-mvc 需要剃刀视图引擎自动完成才能在类库中工作吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4158573/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 00:34:36  来源:igfitidea点击:

Need razor view engine auto-complete to work in a class library?

asp.net-mvcasp.net-mvc-3razorautocompleteintellisense

提问by JarrettV

We have a modular architecture where we have some views (cshtml) files in a separate project (class library). How can we get the syntax highlighting andautocomplete to work when the project isn't an MVC project?

我们有一个模块化架构,我们在一个单独的项目(类库)中有一些视图(cshtml)文件。我们怎样才能得到语法高亮和当项目不是 MVC 项目时自动完成工作?

Please note that the class library has controllers, views, models etc. It just doesn't have the web.config, global.asax, etc that a normal mvc project would have.

请注意,类库具有控制器、视图、模型等。它只是没有普通 mvc 项目所具有的 web.config、global.asax 等。

The intellisense works for everything but the so important model: screenshot of model error

智能感知适用于除如此重要的模型之外的所有内容: 模型错误截图

With MVC3 RTM, if you hover over the Model, you can now get a better error message:

使用 MVC3 RTM,如果您将鼠标悬停在模型上,您现在可以获得更好的错误消息:

C:\...\Index.cshtml: ASP.NET runtime error: There is no build provider registered for the extension '.cshtml'. You can register one in the <compilation><buildProviders> section in the machine.config or web.config. Make sure is has a BuildProviderAppliesToAttribute attribute which includes the value 'Web' or 'All'.

C:\...\Index.cshtml: ASP.NET 运行时错误:没有为扩展名“.cshtml”注册的构建提供程序。您可以在 machine.config 或 web.config 的 <compilation><buildProviders> 部分注册一个。确保具有 BuildProviderAppliesToAttribute 属性,其中包含值“Web”或“All”。

So I added this:

所以我添加了这个:

<compilation>
    <assemblies>
      <add assembly="System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    </assemblies>
    <buildProviders>
      <add
         extension=".cshtml"
         type="System.Web.WebPages.Razor.RazorBuildProvider, System.Web.WebPages.Razor"/>
    </buildProviders>
  </compilation>

Then after adding the build provider, this error message appears:

然后在添加构建提供程序后,出现此错误消息:

C:\...\Index.cshtml: ASP.NET runtime error: Could not load file or assembly 'System.Web.WebPages.Razor' or one of its dependencies. The system cannot find the file specified. (C:\...\machine.config line 259)

C:\...\Index.cshtml: ASP.NET 运行时错误:无法加载文件或程序集“System.Web.WebPages.Razor”或其依赖项之一。该系统找不到指定的文件。(C:\...\machine.config 第 259 行)

回答by Deepak

The webconfig from this postwill work. I've copied it below (for posterity):

这篇文章中的 webconfig将起作用。我在下面复制了它(为了后代):

<?xml version="1.0"?>
<configuration>

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

    <system.web.webPages.razor>
        <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <pages pageBaseType="System.Web.Mvc.WebViewPage">
            <namespaces>
                <add namespace="System.Web.Mvc" />
                <add namespace="System.Web.Mvc.Ajax" />
                <add namespace="System.Web.Mvc.Html" />
                <add namespace="System.Web.Routing" />
            </namespaces>
        </pages>
    </system.web.webPages.razor>

    <system.web>
        <compilation targetFramework="4.0">
            <assemblies>
                <add assembly="System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
                <add assembly="System.Web.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
                <add assembly="System.Data.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
                <add assembly="System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
            </assemblies>
        </compilation>
    </system.web>

</configuration>

回答by Omar

For Visual Studio 2012/ASP.NET MVC 4, you need to update the assembly versions and add <add key="webpages:Version" value="2.0.0.0" />to appSettings. Here's what my Web.config looks like:

对于 Visual Studio 2012/ASP.NET MVC 4,您需要更新程序集版本并添加<add key="webpages:Version" value="2.0.0.0" />appSettings. 这是我的 Web.config 的样子:

<?xml version="1.0"?>
<configuration>

  <configSections>
    <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="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
      <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>

  <system.web.webPages.razor>
    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <pages pageBaseType="System.Web.Mvc.WebViewPage">
      <namespaces>
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Routing" />
      </namespaces>
    </pages>
  </system.web.webPages.razor>

  <appSettings>
    <add key="webpages:Version" value="2.0.0.0" />
  </appSettings>

  <system.web>
    <compilation targetFramework="4.5">
      <assemblies>
        <add assembly="System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
        <add assembly="System.Web.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
        <add assembly="System.Data.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
        <add assembly="System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
      </assemblies>
    </compilation>
  </system.web>

</configuration>

回答by BlueMonkMN

I have followed Jammer's suggestion and am documenting what I believe is the minimal set of actions to get a project suitable for serving as an ASP.NET MVC4 class library project. This was done in Visual Studio 2012 Update 4, and was targeting VB.Net. I may later include documentation for doing something similar in Visual Studio 2013 if I get a chance. Here are the actions I took:

我遵循了 Jammer 的建议,并记录了我认为使项目适合用作 ASP.NET MVC4 类库项目的最少操作集。这是在 Visual Studio 2012 Update 4 中完成的,目标是 VB.Net。如果有机会,我稍后可能会包含在 Visual Studio 2013 中执行类似操作的文档。以下是我采取的行动:

  1. Create a new blank MVC4 project with a separate directory for the solution (so you can clearly see that the packagesdirectory with updated versions of reference files is part of the project).
  2. Add a dummy controller, view and model to test it (which entailed copying the output DLL and view to another web application after building the class library).
  3. Delete the following files from the project:
    • The whole App_Startfolder
    • The whole App_Datafolder and any other empty folders (my Mercurial history didn't make this visible so I'm going from memory).
    • Global.asax
    • Global.asax.vb
    • The Web.configfile in the root and the dependent Web.Debug.configand Web.Release.configfiles. (Do notdelete Web.configfrom the Views folder.)
  4. Delete the following sections from the Web.configfile in the Views folder:
    • appSettings
    • system.web
    • system.webServer
    • You may also delete lines <add namespace="System.Web.Mvc.Ajax" />and <add namespace="System.Web.Routing" />
  5. Remove the following references from the project (* starred references were version-specific references that went into the packagesdirectory and have Copy Localand Specific Versionset to True).
    • System.Web.Entity
    • System.Web.ApplicationServices
    • System.ComponentModel.DataAnnotations
    • System.Data.DataSetExtensions
    • System.Web.Extensions
    • System.Web.Extensions.Design
    • System.Xml.Linq
    • System.Web.Abstractions
    • System.Web.Routing
    • System.Configuration
    • System.Web.Services
    • System.EnterpriseServices
    • Microsoft.Web.Infrastructure(1.0.0.0) *
    • Microsoft.Web.Mvc.FixedDisplayModes(1.0.0) *
    • Newtonsoft.Json(4.5.11) *
    • System.Net.Http(2.0.20710.0) *
    • System.Net.Http.Formatting(4.0.20710.0) *
    • System.Net.Http.WebRequest(2.0.20710.0) *
    • System.Web.Helpers(2.0.20710.0) *
    • System.Web.Http(4.0.20710.0) *
    • System.Web.Http.WebHost(4.0.20710.0) *
  6. Remove the following project-wide Imports from the project settings:
    • System.Xml.Linq
    • System.Collections.Specialized
    • System.Configuration
    • System.Web.Caching
    • System.Web.Mvc.Ajax
    • System.Web.Routing
    • System.Web.SessionState
    • System.Web.Security
    • System.Web.Profile
    • System.Web.UI
    • System.Web.UI.WebControls
    • System.Web.UI.WebControls.WebParts
    • System.Web.UI.HtmlControls
  7. Remove the following from packages.config:
    • <package id="Microsoft.AspNet.Mvc.FixedDisplayModes" version="1.0.0" targetFramework="net40" />
    • <package id="Microsoft.AspNet.WebApi" version="4.0.20710.0" targetFramework="net40" />
    • <package id="Microsoft.AspNet.WebApi.Client" version="4.0.20710.0" targetFramework="net40" />
    • <package id="Microsoft.AspNet.WebApi.Core" version="4.0.20710.0" targetFramework="net40" />
    • <package id="Microsoft.AspNet.WebApi.WebHost" version="4.0.20710.0" targetFramework="net40" />
    • <package id="Microsoft.Net.Http" version="2.0.20710.0" targetFramework="net40" />
    • <package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net40" />
    • <package id="Newtonsoft.Json" version="4.5.11" targetFramework="net40" />
  8. Delete the following sub-directories from the packages folder:
    • Microsoft.AspNet.Mvc.FixedDisplayModes.1.0.0
    • Microsoft.AspNet.WebApi.4.0.20710.0
    • Microsoft.AspNet.WebApi.Client.4.0.20710.0
    • Microsoft.AspNet.WebApi.Core.4.0.20710.0
    • Microsoft.AspNet.WebApi.WebHost.4.0.20710.0
    • Microsoft.Net.Http.2.0.20710.0
    • Microsoft.Web.Infrastructure.1.0.0.0
    • Newtonsoft.Json.4.5.11
  1. 为解决方案创建一个具有单独目录的新空白 MVC4 项目(以便您可以清楚地看到packages包含更新版本的参考文件的目录是项目的一部分)。
  2. 添加一个虚拟控制器、视图和模型来测试它(这需要在构建类库后将输出 DLL 和视图复制到另一个 Web 应用程序)。
  3. 从项目中删除以下文件:
    • 整个App_Start文件夹
    • 整个App_Data文件夹和任何其他空文件夹(我的 Mercurial 历史记录没有使这可见,所以我从记忆中去)。
    • Global.asax
    • Global.asax.vb
    • Web.config根和相关的文件Web.Debug.configWeb.Release.config文件。(做删除Web.config从浏览文件夹。)
  4. Web.configViews 文件夹中的文件中删除以下部分:
    • appSettings
    • system.web
    • system.webServer
    • 您也可以删除行<add namespace="System.Web.Mvc.Ajax" /><add namespace="System.Web.Routing" />
  5. 从项目中删除以下引用(* 带星号的引用是进入packages目录Copy LocalSpecific Version设置为的特定于版本的引用True)。
    • System.Web.Entity
    • System.Web.ApplicationServices
    • System.ComponentModel.DataAnnotations
    • System.Data.DataSetExtensions
    • System.Web.Extensions
    • System.Web.Extensions.Design
    • System.Xml.Linq
    • System.Web.Abstractions
    • System.Web.Routing
    • System.Configuration
    • System.Web.Services
    • System.EnterpriseServices
    • Microsoft.Web.Infrastructure(1.0.0.0) *
    • Microsoft.Web.Mvc.FixedDisplayModes(1.0.0) *
    • Newtonsoft.Json(4.5.11) *
    • System.Net.Http(2.0.20710.0) *
    • System.Net.Http.Formatting(4.0.20710.0) *
    • System.Net.Http.WebRequest(2.0.20710.0) *
    • System.Web.Helpers(2.0.20710.0) *
    • System.Web.Http(4.0.20710.0) *
    • System.Web.Http.WebHost(4.0.20710.0) *
  6. 从项目设置中删除以下项目范围的导入:
    • System.Xml.Linq
    • System.Collections.Specialized
    • System.Configuration
    • System.Web.Caching
    • System.Web.Mvc.Ajax
    • System.Web.Routing
    • System.Web.SessionState
    • System.Web.Security
    • System.Web.Profile
    • System.Web.UI
    • System.Web.UI.WebControls
    • System.Web.UI.WebControls.WebParts
    • System.Web.UI.HtmlControls
  7. 从 中删除以下内容packages.config
    • <package id="Microsoft.AspNet.Mvc.FixedDisplayModes" version="1.0.0" targetFramework="net40" />
    • <package id="Microsoft.AspNet.WebApi" version="4.0.20710.0" targetFramework="net40" />
    • <package id="Microsoft.AspNet.WebApi.Client" version="4.0.20710.0" targetFramework="net40" />
    • <package id="Microsoft.AspNet.WebApi.Core" version="4.0.20710.0" targetFramework="net40" />
    • <package id="Microsoft.AspNet.WebApi.WebHost" version="4.0.20710.0" targetFramework="net40" />
    • <package id="Microsoft.Net.Http" version="2.0.20710.0" targetFramework="net40" />
    • <package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net40" />
    • <package id="Newtonsoft.Json" version="4.5.11" targetFramework="net40" />
  8. 从包文件夹中删除以下子目录:
    • Microsoft.AspNet.Mvc.FixedDisplayModes.1.0.0
    • Microsoft.AspNet.WebApi.4.0.20710.0
    • Microsoft.AspNet.WebApi.Client.4.0.20710.0
    • Microsoft.AspNet.WebApi.Core.4.0.20710.0
    • Microsoft.AspNet.WebApi.WebHost.4.0.20710.0
    • Microsoft.Net.Http.2.0.20710.0
    • Microsoft.Web.Infrastructure.1.0.0.0
    • Newtonsoft.Json.4.5.11

This leaves me with the following:

这给我留下了以下内容:

  1. A VB.Net class library project targeting .NET Framework 4 (although I had intended this to be 4.5 - I think either works).
  2. The following non-default .NET references (starred references must have Copy Localand Specific Versionset to True):
    • System.Web
    • packages\Microsoft.AspNet.Mvc.4.0.20710.0\lib\net40\System.Web.Mvc.dll *
    • packages\Microsoft.AspNet.Razor.2.0.20715.0\lib\net40\System.Web.Razor.dll *
    • packages\Microsoft.AspNet.WebPages.2.0.20710.0\lib\net40\System.Web.WebPages.dll *
    • packages\Microsoft.AspNet.WebPages.2.0.20710.0\lib\net40\System.Web.WebPages.Deployment.dll *
    • packages\Microsoft.AspNet.WebPages.2.0.20710.0\lib\net40\System.Web.WebPages.Razor.dll *
  3. The following non-default imports:
    • System.Web
    • System.Web.Mvc
    • System.Web.Mvc.Html
  4. The following project files/structure:
    • Controllersfolder containing CustomUIController.vb
    • Modelsfolder containing CustomUIModel.vb
    • Viewsfolder containing CustomUIfolder, containing Index.vbhtml
  5. Web.configfile in the Views folder. See below for content.
  6. packages.configfile in the root of the project. See below for content.
  1. 一个面向 .NET Framework 4 的 VB.Net 类库项目(虽然我原本打算将其设为 4.5 - 我认为两者都可以)。
  2. 以下非默认 .NET 引用(带星号的引用必须具有Copy LocalSpecific Version设置为True):
    • 系统.Web
    • 包\Microsoft.AspNet.Mvc.4.0.20710.0\lib\net40\System.Web.Mvc.dll *
    • 包\Microsoft.AspNet.Razor.2.0.20715.0\lib\net40\System.Web.Razor.dll *
    • 包\Microsoft.AspNet.WebPages.2.0.20710.0\lib\net40\System.Web.WebPages.dll *
    • 包\Microsoft.AspNet.WebPages.2.0.20710.0\lib\net40\System.Web.WebPages.Deployment.dll *
    • 包\Microsoft.AspNet.WebPages.2.0.20710.0\lib\net40\System.Web.WebPages.Razor.dll *
  3. 以下非默认导入:
    • 系统.Web
    • 系统.Web.MVC
    • 系统.Web.Mvc.Html
  4. 以下项目文件/结构:
    • Controllers文件夹包含 CustomUIController.vb
    • Models文件夹包含 CustomUIModel.vb
    • Views文件夹包含CustomUI文件夹,包含Index.vbhtml
  5. Web.config视图文件夹中的文件。内容见下文。
  6. packages.config项目根目录下的文件。内容见下文。

The contents of my files are as follows:

我的文件内容如下:

Web.config

网页配置

<?xml version="1.0"?>

<configuration>
  <configSections>
    <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="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
      <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>

  <system.web.webPages.razor>
    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <pages pageBaseType="System.Web.Mvc.WebViewPage">
      <namespaces>
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Html" />
      </namespaces>
    </pages>
  </system.web.webPages.razor>

</configuration>

packages.config

包.config

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="Microsoft.AspNet.Mvc" version="4.0.20710.0" targetFramework="net40" />
  <package id="Microsoft.AspNet.Razor" version="2.0.20715.0" targetFramework="net40" />
  <package id="Microsoft.AspNet.WebPages" version="2.0.20710.0" targetFramework="net40" />
</packages>

CustomUIController.vb

自定义UIController.vb

Imports System.Web.Mvc

Public Class CustomUIController
   Inherits Controller

   Public Function Index() As ActionResult
      Return View()
   End Function
End Class

CustomUIModel.vb

自定义UIModel.vb

Public Class CustomUIModel
   Public Property Name As String
   Public Property Value As Decimal
End Class

Index.vbhtml

索引.vbhtml

@ModelType CustomTemplate.CustomUIModel

@Html.LabelFor(Function(m) m.Name)

At this point I am able to work with Intellisense assisting me in the .vbhtml views and the .vb classes, build the project, then copy just the views to the primary application's deployed Viewsfolder (in the appropriate sub-directory), and the project's primary output DLL to the primary application's deployed bindirectory (the dependent DLL files are already there).

在这一点上,我可以使用 Intellisense 协助我处理 .vbhtml 视图和 .vb 类,构建项目,然后仅将视图复制到主应用程序的部署Views文件夹(在适当的子目录中),以及项目的主输出 DLL 到主应用程序的部署bin目录(依赖 DLL 文件已经存在)。

Edit:

编辑:

After following the process on another system to validate it and how it works for .NET 4.5 and VS 2013, I have noticed the following:

在另一个系统上遵循该过程以验证它以及它如何适用于 .NET 4.5 和 VS 2013 之后,我注意到以下几点:

  1. I think I neglected to mention that the reference to System.Web.DynamicDatacan be removed.
  2. In VS 2013 and/or .NET 4.5, some versions change:
    • packages\Microsoft.AspNet.Mvc.4.0.30506.0\lib\net40\System.Web.Mvc.dll
    • packages\Microsoft.AspNet.Razor.2.0.30506.0\lib\net40\System.Web.Razor.dll
    • packages\Microsoft.AspNet.WebPages.2.0.30506.0\lib\net40\System.Web.WebPages.dll
    • packages\Microsoft.AspNet.WebPages.2.0.30506.0\lib\net40\System.Web.WebPages.Deployment.dll
    • packages\Microsoft.AspNet.WebPages.2.0.30506.0\lib\net40\System.Web.WebPages.Razor.dll
  3. I don't know if I made a strong enough point of it above, but the references to private (Copy Local) DLLs dohave to be set to Copy Local, and/or dohave to use the version provided in the packages folder when creating a project from the MVC4 template. I don't know why, but the .NET standard versions (non-private) don't seem to work, as far as Intellisense is concerned at least.
  4. Best way to check if things are still "OK" as far as the IDE is concerned is to close the solution, delete the binand objfolders in the custom project's output, re-load the solution, put the cursor on LabelForin the Index.vbhtml file, and press the F12 key to see if it takes you to the Object Browser.
  1. 我想我忽略了提到System.Web.DynamicData可以删除的引用。
  2. 在 VS 2013 和/或 .NET 4.5 中,某些版本发生了变化:
    • 包\Microsoft.AspNet.Mvc.4.0.30506.0\lib\net40\System.Web.Mvc.dll
    • 包\Microsoft.AspNet.Razor.2.0.30506.0\lib\net40\System.Web.Razor.dll
    • 包\Microsoft.AspNet.WebPages.2.0.30506.0\lib\net40\System.Web.WebPages.dll
    • 包\Microsoft.AspNet.WebPages.2.0.30506.0\lib\net40\System.Web.WebPages.Deployment.dll
    • 包\Microsoft.AspNet.WebPages.2.0.30506.0\lib\net40\System.Web.WebPages.Razor.dll
  3. 我不知道如果我做了它上面一个足够强大的点,但私人(复制本地)的DLL的引用必须被设置为复制本地和/或必须使用在包装中提供的版本文件夹时从 MVC4 模板创建一个项目。我不知道为什么,但至少就 Intellisense 而言,.NET 标准版本(非私有)似乎不起作用。
  4. 就 IDE 而言,检查事情是否仍然“正常”的最佳方法是关闭解决方案,删除自定义项目输出中的binobj文件夹,重新加载解决方案,将光标放在LabelForIndex.vbhtml 文件中,然后按 F12 键查看它是否会将您带到对象浏览器。

Because of the new versions, the packages file is different:

由于新版本,packages 文件有所不同:

Packages.config

包.config

<packages>
  <package id="Microsoft.AspNet.Mvc" version="4.0.30506.0" targetFramework="net45" />
  <package id="Microsoft.AspNet.Razor" version="2.0.30506.0" targetFramework="net45" />
  <package id="Microsoft.AspNet.WebPages" version="2.0.30506.0" targetFramework="net45" />
</packages>

回答by Rap

Alright, this is a longshot, but I ran across the same problem. Are you using JetBrains Resharper?

好吧,这是一个远景,但我遇到了同样的问题。您在使用 JetBrains Resharper 吗?

Resharper overrides VS's intellisense engine but it doesn't understand Razor syntax. You simply have to tell VS to rely on its own intellisense rather than Resharper's.

Resharper 覆盖了 VS 的智能感知引擎,但它不理解 Razor 语法。您只需要告诉 VS 依靠它自己的智能感知而不是 Resharper 的智能感知。

In VS2010, go Resharper - Options - Intellisense - General. Then check the Visual Studio radio button.

在 VS2010 中,转到 Resharper - Options - Intellisense - General。然后检查 Visual Studio 单选按钮。

Worked great for me.

对我很有用。

回答by Jammer

None of the solutions I could find online or on SO fixed this for me.

我在网上或在 SO 上找到的解决方案都没有为我解决这个问题。

May seem like a sledgehammer to crack a nut but I created an MVC 4 application project instead of a class library and ripped out everything I didn't need. Intellisense and @modelworking fine.

可能看起来像一把大锤来破解坚果,但我创建了一个 MVC 4 应用程序项目而不是一个类库,并删除了我不需要的所有东西。智能感知并且@model工作正常。