Razor 语法在编译前在视图中给出错误(无智能感知) [英] Razor Syntax gives errors in View before compiling (No intellisense)

查看:31
本文介绍了Razor 语法在编译前在视图中给出错误(无智能感知)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在浏览 NuGet 包的列表,并且正在更新所有东西,例如引导程序、jQuery 等.但是在我这样做的时候,我不假思索地更新了 Microsoft.Owin 和另一个包(可能已经是 EF 或其他东西),这是一个主要版本更新...呜呜.

I was going through the list of NuGet Packages and I was updating everything things like bootstrap, jQuery, etc. but while I was doing that, I unthinkingly updated Microsoft.Owin and another package (might have been EF or something else) and it was a major version update...woops.

我不得不通过删除我的项目、从源代码管理中获取旧版本并将所有内容恢复到正确版本来解决问题.一切都恢复正常,我还有最后一个问题.Razor 语法.

I had to jump through hoops by deleting my project, getting an older version from source control and getting everything back to the right versions. Everything is back to normal and I have one last issue. Razor Syntax.

当我打开我的任何视图时...在项目的任何地方,我得到以下信息:

When I open up any of my views...anywhere in the project, I get this:

请注意使用 Razor 的任何内容都标有红色错误波浪线.他们每个人都说当前上下文中不存在名称''".

Notice how anything using Razor is marked with red error squiggle. They each say "The name '' does not exist in the current context".

当我关闭文件时,没有显示任何错误.此外,当我运行该项目时,没有任何问题,一切正常.问题是,我没有智能感知,它也无法识别我的任何剃刀助手.

When I close the file, no errors are shown. Also, when I run the project, there are no issues and everything works PERFECT. The problem is, I have no intellisense and it doesn't recognize any of my razor helpers.

以下是我的项目的规格:

Here are the specifications for my project:

EntityFramework 6.0
Microsoft.Owin 2.1.0.0
Owin 1.0.0
System.Web.Mvc 5.2.2.0
System.Web.Razor 3.0.0.0

在我的包文件夹中,我也有:

In my packages folder I also have:

Microsoft.AspNet.Mvc.5.2.2
Microsoft.AspNet.Razor.3.2.2
Microsoft.WebPages.3.2.2

我修改了我的 WebConfig 以修复多个错误,现在它包含以下代码:

I've modified my WebConfig to fix multiple errors and it now has the following code in it:

<compilation debug="true" targetFramework="4.5.1">
      <assemblies>
        <add assembly="System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <add assembly="System.Web.Helpers, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <add assembly="System.Web.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <add assembly="System.Web.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <add assembly="System.Web.Mvc, Version=5.2.2.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <add assembly="System.Web.WebPages, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
      </assemblies>
    </compilation>

及以下:

我有这些:

<dependentAssembly>
        <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.AspNet.Identity.Core" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.5.0.0" newVersion="4.5.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin.Security.OAuth" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-2.1.0.0" newVersion="2.1.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-2.1.0.0" newVersion="2.1.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin.Security" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-2.1.0.0" newVersion="2.1.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin.Security.Cookies" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-2.1.0.0" newVersion="2.1.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.WindowsAzure.Storage" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.2.1.0" newVersion="3.0.3.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Razor" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-5.2.2.0" newVersion="5.2.2.0" />
      </dependentAssembly>

在我的项目中的每个 View 文件夹上,我在 Web.Config 文件中都有以下代码:

And on each of the View folders in my project I have the following code inside of the Web.Config file:

<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.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.Optimization"/>
        <add namespace="System.Web.Routing" />
        <add namespace="IcapOrg" />
      </namespaces>
    </pages>
  </system.web.webPages.razor>

我尝试过卸载它、重新安装、升级、降级、删除和添加程序集,以及更多我开始忘记的事情.我不确定从这里往哪里继续.我可以继续编码,但如果没有智能感知,这将是一种真正的痛苦.在此先感谢您的帮助.

I have tried uninstalling it, re-installing, upgrading, downgrading, removing and adding assemblies, and many more things I've started to forget. I'm not really sure where to move on from here. I can keep coding but it's just going to be a real pain without intellisense. Thank you in advance for any help.

推荐答案

找了几天,终于找到这个页面:MVC Razor 视图 Intellisense 在 VS 2013/2015/2017 中被破坏

After days of searching, I finally found this page: MVC Razor view Intellisense broken in VS 2013/2015/2017

在我的 Views > Web.Config 文件中,我发现了以下代码:

In my Views > Web.Config file I found this code:

 <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" />
        <add namespace="System.Web.Optimization" />
        <add namespace="System.Web.Helpers"/>
        <add namespace="IcapOrg" />
      </namespaces>
    </pages>
  </system.web.webPages.razor>

如您所见,其中包含错误的 MVC 版本.我之前只检查了 标签以获取 RAZOR 的正确版本,但我忽略了 MVC 也需要正确版本的事实.将版本切换为:

Which, as you can see, has the wrong version of MVC on it. I had previously only checking the <sectionGroup> tags for the proper version of RAZOR but I neglected the fact that MVC also needs the proper version. Switching the version to:

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

解决了我的问题.这提醒我要始终非常仔细地查看我的代码.

Solved my problem. This reminds me to always look over my code VERY carefully.

注意:正如您在问题中看到的,版本显示为 5.0.0,而这个显示为 4.0.0.我的不同视图文件夹中的不同 Web.Config 文件都有不同的版本.极其诡异.

NOTE: As you can see in the question, the version says 5.0.0, and this one says 4.0.0. Different Web.Config files in my different view folders all had different versions written down. Extremely weird.

这篇关于Razor 语法在编译前在视图中给出错误(无智能感知)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆