asp.net-mvc CS0234:Mvc 在 System.Web 命名空间中不存在

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

CS0234: Mvc does not exist in the System.Web namespace

asp.net-mvcasp.net-mvc-4

提问by user266909

I converted a ASP.net 4 webform project to Asp.net MVC4 according to Chapter 13 of the Professional ASP.NET 3.5 MVC, by Scott Hanselmen, Phil Haack, and Rob Conery, Published by Wiley Publishing, Inc. (ISBN: 978-0-470-38461-9). I also followed this blog. Now I can add Controllers, Views, etc. All the references are set properly. No build errors. But upon launching the converted project, I got compilation errors. I have mvc3 installed on the same machine. I even changed the reference to that. It still complain. Could you help? Thanks.

我根据专业 ASP.NET 3.5 MVC 的第 13 章将 ASP.net 4 webform 项目转换为 Asp.net MVC4,作者是 Scott Hanselmen、Phil Haack 和 Rob Conery,由 Wiley Publishing, Inc. 出版(ISBN:978- 0-470-38461-9)。我也关注了这个博客。现在我可以添加控制器、视图等。所有引用都设置正确。没有构建错误。但是在启动转换后的项目时,我遇到了编译错误。我在同一台机器上安装了 mvc3。我什至改变了对那个的引用。它仍然抱怨。你能帮忙吗?谢谢。

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. 

Compiler Error Message: CS0234: The type or namespace name 'Mvc' does not exist in the namespace 'System.Web' (are you missing an assembly reference?)

Source Error:

Line 109:            <namespaces>
Line 110:                <add namespace="System.Web.Helpers"/>
Line 111:                <add namespace="System.Web.Mvc"/>
Line 112:                <add namespace="System.Web.Mvc.Ajax"/>
Line 113:                <add namespace="System.Web.Mvc.Html"/> 

Source File: c:\Users\Jon\Documents\Visual Studio 2012\Projects\CMT\Apps\Branches.0\Web.config    Line: 111 

采纳答案by user2868064

This problem can happen when you deploy your web application to a server, so you must check if you already installed MVC3.

当您将 Web 应用程序部署到服务器时可能会发生此问题,因此您必须检查您是否已经安装了 MVC3。

Check if the folder C:\Program Files\Microsoft ASP.NET\ASP.NET MVC 3exists.

检查文件夹是否C:\Program Files\Microsoft ASP.NET\ASP.NET MVC 3存在。

If it doesn't exist, you need to install it from http://www.microsoft.com/en-us/download/details.aspx?id=1491

如果它不存在,您需要从http://www.microsoft.com/en-us/download/details.aspx?id=1491安装它



If you wont to install you can add all DLLs locally in bin folder and add references to them this work fine if you host on server don't deploy ASP.NET Web Pages or MVC3.

如果您不想安装,您可以在 bin 文件夹中本地添加所有 DLL 并添加对它们的引用,如果您在服务器上托管而不部署 ASP.NET 网页或 MVC3,则此工作正常。

回答by curiousBoy

I had the same problem and I had solved it with:

我有同样的问题,我已经解决了:

1.Right click to solution and click 'Clean Solution'

1.右键单击解决方案并单击 'Clean Solution'

2.Click 'References'folder in solution explorer and select the problem reference (in your case it seems System.Web.Mvc) and then right click and click 'Properties'.

2.单击'References'解决方案资源管理器中的文件夹并选择问题参考(在您的情况下为 System.Web.Mvc),然后右键单击并单击'Properties'

3.In the properties window, make sure that the 'Copy Local'property is set to 'True'

3.在属性窗口中,确保将'Copy Local'属性设置为'True'

This worked for me. Hope it works for someone else

这对我有用。希望它适用于其他人

Helpful Note: As it has mentioned in comments by @Vlad:

有用的说明:正如@Vlad 在评论中提到的:

If it is already set to True:

如果它已经设置为 True:

  1. Set it False
  2. Set it True again
  3. Rebuild
  1. 设置为假
  2. 再次设置为真
  3. 重建

回答by amhed

Check your runtime tag inside the web.config, and verify you have something like this declared:

检查 web.config 中的运行时标记,并验证您是否声明了如下内容:

<runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
      </dependentAssembly>
.....
</runtime>

回答by Jon Koeter

I tried all these answers, even closed Visual Studio and deleted all bin directories.

我尝试了所有这些答案,甚至关闭了 Visual Studio 并删除了所有 bin 目录。

After starting it up again the MVC reference appeared to have a yellow exclamation mark on it, so I removed it and added it again.

再次启动后,MVC 引用上似乎有一个黄色的感叹号,因此我将其删除并再次添加。

Now it works, without copy local.

现在它可以工作,无需复制本地。

回答by glautrou

None of previous answers worked for me.

以前的答案都不适合我。

I noticed that my project was referencing another project using the System.Web.Mvcreference from the .NET Framework.

我注意到我的项目正在使用System.Web.Mvc.NET Framework 中的引用引用另一个项目。

I just deleted that assembly and added the "Microsoft.AspNet.Mvc" NuGet package and that fixed my problem.

我刚刚删除了该程序集并添加了“ Microsoft.AspNet.Mvc” NuGet 包,这解决了我的问题。

回答by eric_eri

add Microsoft.AspNet.Mvc nuget package.

添加 Microsoft.AspNet.Mvc nuget 包。

回答by karim

You need to include the reference to the assembly System.Web.Mvc in you project.

您需要在项目中包含对程序集 System.Web.Mvc 的引用。

you may not have the System.Web.Mvc in your C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.0

您的 C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.0 中可能没有 System.Web.Mvc

So you need to add it and then to include it as reference to your projrect

因此,您需要添加它,然后将其包含为对您的项目的引用

回答by KBrianJ

I had this problem, but all the applications in IIS were broken when they had been working previously. None of the marked solutions helped me. The problem ended up being an extra copy of web.config had been introduced to my root directory. I removed that file and problem solved.

我遇到了这个问题,但是 IIS 中的所有应用程序在以前工作时都已损坏。没有一个明显的解决方案对我有帮助。问题最终是 web.config 的额外副本已被引入我的根目录。我删除了那个文件,问题解决了。