asp.net-mvc Razor 编译器警告/错误 - ASP.NET MVC 4

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

Razor Compiler Warning/Errors - ASP.NET MVC 4

asp.net-mvcrazorcompiler-errorsasp.net-mvc-4compiler-warnings

提问by garfbradaz

I have an issue which seems to have been reported here:

我有一个似乎已在此处报告的问题:

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

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

My issue is the following Warning:

我的问题是以下警告:

G:\Accountable\Accountable\Views\LedgerUser\EditorTemplates\LedgerServiceViewModel.cshtml: ASP.NET runtime error: There is no build provider registered for the extension '.cshtml'. You can register one in the section in machine.config or web.config. Make sure is has a BuildProviderAppliesToAttribute attribute which includes the value 'Web' or 'All'.

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

This issue is apparent in all my views. Sample images below.

这个问题在我所有的观点中都很明显。下面的示例图像。

enter image description here

在此处输入图片说明

enter image description here

在此处输入图片说明

enter image description here

在此处输入图片说明

Now all posts and references online that I have found are referencing to adding various Web.config (as mentioned in the previous post) options, but this doesn't seem to work, as I have these configurations defaulted in the project. Here are my Web Configs:

现在我发现的所有在线帖子和参考都引用了添加各种 Web.config(如上一篇文章中所述)选项,但这似乎不起作用,因为我在项目中默认了这些配置。这是我的网络配置:

Main Web.config

主 Web.config

View root Web Config

查看根 Web 配置

I think this issue maybe while im also getting the following compiler errors on the Views:

我认为这个问题可能同时我也在视图上收到以下编译器错误:

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

当前上下文中不存在名称“Html”

enter image description here

在此处输入图片说明

What am I doing wrong?

我究竟做错了什么?

回答by garfbradaz

Arrrgghhh i had missed the following from the ROOT Web.config:

Arrrgghhh 我错过了 ROOT Web.config 中的以下内容:

 <compilation debug="true" targetFramework="4.0">
  <!-- New -->
  <assemblies>
    <add assembly="System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <add assembly="System.Web.Helpers, Version=1.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=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <add assembly="System.Web.WebPages, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
  </assemblies>
</compilation>

This allowed fixed the error and allowed me to use Intellisense within the View.

这允许修复错误并允许我在视图中使用智能感知。

回答by abatishchev

Putting back the following lines in root Web.config fixed:

在根 Web.config 中放回以下几行固定:

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

回答by Mukus

I got it working by simply unloading and reloading the project. All the namespaces above were already there in the root web.config and I was still getting this issue.

我通过简单地卸载和重新加载项目来让它工作。上面的所有命名空间都已经存在于根 web.config 中,我仍然遇到这个问题。

回答by Fernando Neira

Removing the temporary asp.net framework files works for me.

删除临时 asp.net 框架文件对我有用。

Stop IIS and remove everything under this folder:

停止 IIS 并删除此文件夹下的所有内容:

C:\Windows\Microsoft.NET\Framework64\[framework version]\Temporary ASP.NET Files

C:\Windows\Microsoft.NET\Framework64\[框架版本]\Temporary ASP.NET Files

Then restart IIS. Bam!

然后重新启动 IIS。砰!

回答by Kapila Perera

I tried by restart IIS, removed Temporary ASP.NET Files and changing the compilation tag in the web config file. None of them work for me.

我尝试通过重新启动 IIS,删除临时 ASP.NET 文件并更改 Web 配置文件中的编译标记。他们都没有对我来说有效。

I fixed the issue by removing and adding System.Web.MVC to the project.

我通过删除 System.Web.MVC 并将其添加到项目中来解决该问题。

In my case Razor syntax are Highlighted in red.

在我的情况下,Razor 语法以红色突出显示。