asp.net-mvc MVC Razor 视图 Intellisense 在 VS 2013/2015/2017 中被破坏

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22832435/
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 03:37:50  来源:igfitidea点击:

MVC Razor view Intellisense broken in VS 2013/2015/2017

asp.net-mvcvisual-studiorazorintellisense

提问by War

I have an existing project written in VS2010 which when loaded in VS2010 works perfectly.

我有一个用 VS2010 编写的现有项目,在 VS2010 中加载时可以完美运行。

When I load this same project in VS2013 the MVC Razor views contain lots of errors as if the config file is missing from the views folder.

当我在 VS2013 中加载同一个项目时,MVC Razor 视图包含很多错误,就好像视图文件夹中缺少配置文件一样。

It appears to have not loaded the Razor editor correctly using the config files from both the root and the views folder and instead gives me errors like ...

它似乎没有使用根目录和视图文件夹中的配置文件正确加载 Razor 编辑器,而是给了我类似的错误......

The name 'model' does not exist in the current context

and ...

和 ...

'System.Web.WebPages.Html.HtmlHelper' does not contain a definition for 'LabelFor' 
and no extension method 'LabelFor' accepting a first argument of type 
'System.Web.WebPages.Html.HtmlHelper' could be found 
(are you missing a using directive or an assembly reference?)

...

...

Any idea what would cause this?

知道什么会导致这种情况吗?

Edit: Config files as requested ....

编辑:按要求配置文件....

From main web.config file (not all of it as it's way too big to post)

来自主 web.config 文件(不是全部,因为它太大而无法发布)

<runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
        <dependentAssembly>
            <assemblyIdentity name="Ninject" culture="neutral" publicKeyToken="c7192dc5380945e7" />
            <bindingRedirect newVersion="3.0.0.0" oldVersion="0.0.0.0-3.0.0.0" />
        </dependentAssembly>
        <dependentAssembly>
            <assemblyIdentity name="Microsoft.Practices.EnterpriseLibrary.Validation" culture="neutral" publicKeyToken="31bf3856ad364e35" />
            <bindingRedirect newVersion="5.0.505.0" oldVersion="0.0.0.0-5.0.505.0" />
        </dependentAssembly>
        <dependentAssembly>
            <assemblyIdentity name="System.Web" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
            <bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
        </dependentAssembly>
        <dependentAssembly>
            <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
            <bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="3.0.0.0" />
        </dependentAssembly>
        <dependentAssembly>
            <assemblyIdentity name="Microsoft.Practices.ServiceLocation" publicKeyToken="31bf3856ad364e35" culture="neutral" />
            <bindingRedirect oldVersion="0.0.0.0-1.2.0.0" newVersion="1.2.0.0" />
        </dependentAssembly>
    </assemblyBinding>
</runtime>

Config file in "~/Views/" ...

“~/Views/”中的配置文件...

<?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" />
                <add namespace="Emedia.Common.Mvc.Views.Helpers"/>
                <add namespace="Emedia.Common.Mvc.Views.Extensions"/>
        <add namespace="Emedia.Common.Utilities"/>
        <add namespace="Emedia.Common.Utilities.Extensions"/> 
        <add namespace="Emedia.Common.Mvc.Controllers.Helpers"/>
                <add namespace="Emedia.Resources.Service"/>
        <add namespace="Emedia.Subscriber.Controllers"/>
        <add namespace="Emedia.Subscriber.Controllers.ViewModels"/>
            </namespaces>
        </pages>
    </system.web.webPages.razor>

    <appSettings>
        <add key="webpages:Enabled" value="false" />
    </appSettings>

    <system.web>
        <httpHandlers>
            <add path="*" verb="*" type="System.Web.HttpNotFoundHandler"/>
        </httpHandlers>
        <pages
            validateRequest="false"
            pageParserFilterType="System.Web.Mvc.ViewTypeParserFilter, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
            pageBaseType="System.Web.Mvc.ViewPage, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
            userControlBaseType="System.Web.Mvc.ViewUserControl, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
            <controls>
                <add assembly="System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" namespace="System.Web.Mvc" tagPrefix="mvc" />
        </controls>
    </pages>
</system.web>

<system.webServer>
    <validation validateIntegratedModeConfiguration="false" />

    <handlers>
        <remove name="BlockViewHandler"/>
        <add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
    </handlers>
</system.webServer>

回答by viggity

I considered editing @ChrisMoschini's post, but thought it was different enough. My issue was that I started a new MVC5 application, and blindly copied over too many web.config settings from an old MVC3 project I wanted to use as a template/starting point. Doing this caused me to have some invalid versions referenced in my web.config.

我考虑过编辑@ChrisMoschini 的帖子,但认为它已经足够不同了。我的问题是我启动了一个新的 MVC5 应用程序,并从我想用作模板/起点的旧 MVC3 项目中盲目地复制了太多的 web.config 设置。这样做导致我在我的 web.config 中引用了一些无效的版本。

To fix, I created another new MVC5 project and made sure the following config values in my bad project matched the vanilla, unmodified MVC5 app. Again, do not blindly copy these version numbers. Just make sure they match a vanilla MVC app of the version that you're trying to get to work

为了解决这个问题,我创建了另一个新的 MVC5 项目,并确保我的坏项目中的以下配置值与普通的、未修改的 MVC5 应用程序相匹配。同样,不要盲目复制这些版本号。只需确保它们与您尝试开始工作的版本的 vanilla MVC 应用程序相匹配

in root web.config:

在根web.config

<appSettings>
    ...
    <add key="webpages:Version" value="3.0.0.0"/> 
    ...
</appSettings>
<system.web>
    ...
    <compilation debug="true" targetFramework="4.5.1"/>
    <httpRuntime targetFramework="4.5.1"/>
    ...
</system.web>

in the Views\Web.config:

Views\Web.config

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


<system.web.webPages.razor>
  ...
  <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.2.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
  ...
</system.web.webPages.razor>

回答by Jim

A few of my projects had this issues for months. I tried so many workarounds and nothing worked. The issues seemed to be with the projects: the same project will have Intellisense issue across all PCs with Visual Studio. Finally I came through this post: http://www.dennisonpro.info/managing-intellisense-in-razor-views-with-mvc-5-using-custom-builds-in-visual-studio-2013/

我的一些项目几个月来一直存在这个问题。我尝试了很多解决方法,但没有任何效果。问题似乎与项目有关:同一个项目将在所有装有 Visual Studio 的 PC 上出现 Intellisense 问题。最后我通过了这篇文章:http: //www.dennisonpro.info/managing-intellisense-in-razor-views-with-mvc-5-using-custom-builds-in-visual-studio-2013/

In our case the cause was the output directory of all those projects were changed to other than "bin\". By changing the OutputPath back to "bin\" (and cleaning the project, closing then re-opening Visual Studio), I got Intellisense back.

在我们的例子中,原因是所有这些项目的输出目录都被更改为“bin\”以外的目录。通过将 OutputPath 改回“bin\”(并清理项目,关闭然后重新打开 Visual Studio),我恢复了 Intellisense。

The post referenced above also provided a workaround to maintain a separate output directory while still having Intellisense.

上面引用的帖子还提供了一种解决方法,可以在仍具有 Intellisense 的同时维护单独的输出目录。

Hope this helps someone someday.

希望有一天这对某人有所帮助。

回答by SergeyT

MS says that for VS2013 "Intellisense for Razor (CSHTML and VBHTML) files is limited to HTML markup."

MS 表示,对于 VS2013“Razor(CSHTML 和 VBHTML)文件的智能感知仅限于 HTML 标记。”

But if you add these two lines inside each .cshtml the intellisense will work again for MVC3 in VS2013:

但是如果你在每个 .cshtml 中添加这两行,intellisense 将在 VS2013 中再次为 MVC3 工作:

@using System.Web.Mvc.Html
@inherits System.Web.Mvc.WebViewPage<dynamic>

Instead of dynamicyou can put your Model's type.

而不是dynamic你可以把你的模型的类型。

回答by Chris Moschini

I upgraded an MVC3 project to MVC5, and did everything I could to avoid the only true answer to this problem that Microsoft recommends:

我将一个 MVC3 项目升级到 MVC5,并尽我所能避免微软推荐的这个问题的唯一正确答案:

Start over with a new Project, and port the resulting bits over into your existing project.

从一个新项目重新开始,并将结果部分移植到您现有的项目中。

That process is a big waste of time, and it seems like Microsoft should make a real upgrade path viable, but I am here to tell you the problem here is an emotional one not technical: You really do need to just create a new MVC5 project, and replace the following with the result of that new MVC5 project:

这个过程非常浪费时间,看起来微软应该制定一个真正可行的升级路径,但我在这里告诉你这里的问题是一个情感问题而不是技术问题:你真的需要创建一个新的 MVC5 项目,并将以下内容替换为该新 MVC5 项目的结果:

\Packages\*
\Project\Project.csproj
\Project\packages.config
\Project\Web.Config
\Project\Views\Web.Config
\Project\Areas\*\Views\Web.Config

If you don't you'll just go around in circles for eternity trying to find the one setting that's blowing things up. In our case, I had the Web.Configs all identical to the newly-created, Razor Intellisense-working MVC5 project, I had run every variety of upgrade tool I could find, you name it. Intellisense refused to work.

如果你不这样做,你将永远绕圈子,试图找到一个让事情变得更糟的设置。在我们的例子中,我的 Web.Configs 与新创建的、支持 Razor Intellisense 的 MVC5 项目完全相同,我运行了我能找到的各种升级工具,你能想到的。Intellisense 拒绝工作。

After just blowing away the .csproj and web.configs etc, Intellisense came magically back to life. Diffing the 2 sides, none of the answers that I've found anywhere match with what I'm seeing. Web.Configs are almost identical, and the bits that are different should be irrelevant. The main change is really what's in \Packages\ - a lot of older Razor, MVC, and WebPages dlls gone. That could be the trick, but skip that and save yourself a lot of time: Just make a new MVC5 project and dump the above-listed files over. It's the only sane method of upgrading.

在删除 .csproj 和 web.configs 等之后,Intellisense 神奇地恢复了生机。区分两个方面,我在任何地方找到的答案都与我所看到的不符。Web.Configs 几乎相同,不同的位应该是无关紧要的。主要的变化实际上是 \Packages\ 中的内容 - 很多旧的 Razor、MVC 和 WebPages dll 都消失了。这可能是诀窍,但跳过它并节省大量时间:只需创建一个新的 MVC5 项目并转储上面列出的文件。这是唯一明智的升级方法。

回答by Joshua H

I have recently solved this problem myself. I upgraded from MVC4 to MVC5 (specifically 5.1). Upgrading to a newer version of MVC caused this havoc and I spent hours trying to solve it. Minor changes to the Web.Configfile fixed the intellisense issue!

我最近自己解决了这个问题。我从 MVC4 升级到 MVC5(特别是 5.1)。升级到更新版本的 MVC 造成了这个破坏,我花了几个小时试图解决它。对Web.Config文件的小改动修复了智能感知问题!

You said the project works in VS2010, but not 2013? See this answer here.

您说该项目在 VS2010 中有效,但在 2013 中无效?在这里看到这个答案。

I recommend upgrading to MVC5. It's not painful and the upgrade should be pretty seamless.

我建议升级到MVC5。这并不痛苦,升级应该非常无缝。

If you upgrade to MVC5 and you're still not getting intellisense, you need to update the Web.Configfile manually as the upgrade may not do this correctly!

如果您升级到 MVC5 并且仍然没有获得智能感知,则需要Web.Config手动更新文件,因为升级可能无法正确执行此操作!

Here's a modified version of your Web.conifgin the /Viewsfolder that should reflect changes for MVC5.

这是你的修改版本Web.conifg/浏览文件夹应反映MVC5变化。

<?xml version="1.0"?>  
<configuration>
<configSections>
    <sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
        <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
        <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=3.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=5.1.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" />
            <add namespace="Emedia.Common.Mvc.Views.Helpers"/>
            <add namespace="Emedia.Common.Mvc.Views.Extensions"/>
    <add namespace="Emedia.Common.Utilities"/>
    <add namespace="Emedia.Common.Utilities.Extensions"/> 
    <add namespace="Emedia.Common.Mvc.Controllers.Helpers"/>
            <add namespace="Emedia.Resources.Service"/>
    <add namespace="Emedia.Subscriber.Controllers"/>
    <add namespace="Emedia.Subscriber.Controllers.ViewModels"/>
        </namespaces>
    </pages>
</system.web.webPages.razor>

<appSettings>
    <add key="webpages:Enabled" value="false" />
</appSettings>

<system.web>
    <httpHandlers>
        <add path="*" verb="*" type="System.Web.HttpNotFoundHandler"/>
    </httpHandlers>
    <pages
        validateRequest="false"
        pageParserFilterType="System.Web.Mvc.ViewTypeParserFilter, System.Web.Mvc, Version=5.1.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
        pageBaseType="System.Web.Mvc.ViewPage, System.Web.Mvc, Version=5.1.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
        userControlBaseType="System.Web.Mvc.ViewUserControl, System.Web.Mvc, Version=5.1.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
        <controls>
            <add assembly="System.Web.Mvc, Version=5.1.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" namespace="System.Web.Mvc" tagPrefix="mvc" />
    </controls>
</pages>
</system.web>
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />

<handlers>
    <remove name="BlockViewHandler"/>
    <add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
</handlers>
</system.webServer>

If upgrading is not an option, then I'm afraid you will need to continue using VS2010 if you want intellisense support for MVC3. However, I strongly suggest upgrading to MVC5.

如果升级不是一种选择,那么如果您想要对 MVC3 的智能感知支持,恐怕您将需要继续使用 VS2010。但是,我强烈建议升级到 MVC5。

回答by makinitmine

For anyone else who might stumble across this I didn't find any of the previous answers to really solve my problem or not pertain to it. Anyways the way that solved this for me and made intellisense work again was to go to my bin folder and delete all the files in there and then clean/rebuild and it was fixed.

对于可能偶然发现此问题的任何其他人,我没有找到任何以前的答案来真正解决我的问题或与它无关。无论如何,为我解决这个问题并使智能感知再次工作的方法是转到我的 bin 文件夹并删除那里的所有文件,然后清理/重建并修复它。

回答by Simon Bengtsson

When I created a new project intellisense worked fine, but for some reason it didn't work in our current project. The only difference I found in the Views/Web.configfile was that ours had MVC version 5.2.0.0 and a new project had 5.0.0.0. What worked for us was to change this:

当我创建一个新项目时,intellisense 工作正常,但由于某种原因它在我们当前的项目中不起作用。我在Views/Web.config文件中发现的唯一区别是我们的 MVC 版本为 5.2.0.0,而一个新项目的版本为 5.0.0.0。对我们有用的是改变这一点:

<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc,
     Version=5.0.0.0, Culture=neutral, PublicKeyToken=123JHJF56AD364E35" />

to this:

对此:

<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc,
     Version=5.2.0.0, Culture=neutral, PublicKeyToken=123JHJF56AD364E35" />

回答by Andy Brown

I have had this over and over again; I'm on my third new project, and it's driving me mad! I think I may have found the reason for the problem. I'd added files, but not included them in Visual Studio. So I've now chosen to view all files:

我一遍又一遍地经历过这种情况;我正在进行我的第三个新项目,这让我很生气!我想我可能已经找到了问题的原因。我添加了文件,但未将它们包含在 Visual Studio 中。所以我现在选择查看所有文件:

enter image description here

在此处输入图片说明

Then for all my folders which I'd added, I include them:

然后对于我添加的所有文件夹,我将它们包括在内:

enter image description here

在此处输入图片说明

The problem is that this only seems to show up when you open an MVC site as a project, and not when you open it as a website.

问题是,这似乎只有在您将 MVC 站点作为项目打开时才会出现,而在您将其作为网站打开时才会出现。

回答by Norman

A combination of the following helped to solve the problem for me:

以下组合有助于解决我的问题:

  • Creating a new MVC Project
  • Comparing the versions in the views web.config files of the existing and new projects
  • Fixing the versions accordingly (see below)
  • Deleting all files in the bin folder
  • Cleaning the solution
  • and finally rebuilding the solution
  • 创建一个新的 MVC 项目
  • 比较现有项目和新项目的视图 web.config 文件中的版本
  • 相应地修复版本(见下文)
  • 删除bin文件夹中的所有文件
  • 清洁溶液
  • 最后重建解决方案
<configuration>
  <configSections>
    <sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
      <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
      <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=3.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=5.2.3.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.Optimization"/>
        <add namespace="System.Web.Routing" />
      </namespaces>
    </pages>
  </system.web.webPages.razor>

  <appSettings>
    <add key="webpages:Version" value="3.0.0.0"/>
    <add key="webpages:Enabled" value="false" />
  </appSettings>
  [...]

回答by emragins

This question has been resolved, but I'm adding this for future folks, since none of the above worked for me:

这个问题已经解决,但我要为未来的人添加这个,因为以上都不适合我:

Try running Visual Studio as an administrator.

尝试以管理员身份运行 Visual Studio。

Somehow when I tried to delete my nuget packages (which contain all the required references, such as System.Web.Mvc, I was told I need permission from MyPC\Me. Ridiculous! (I am the only user and only admin...) In any case, running as an admin at least let me access those files which fixed intellisense.

不知何故,当我试图删除我的 nuget 包(其中包含所有必需的引用,例如 System.Web.Mvc,我被告知我需要 MyPC\Me 的许可。荒谬!(我是唯一的用户,也是唯一的管理员...... ) 无论如何,以管理员身份运行至少可以让我访问那些修复了智能感知的文件。