C# 命名空间“System.Web.Mvc”中不存在类型或命名空间名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11071392/
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
The type or namespace name does not exist in the namespace 'System.Web.Mvc'
提问by J.Olufsen
Buiding MVC3 solution went well but have got an error in browser:
构建 MVC3 解决方案进展顺利,但在浏览器中出现错误:
Compiler Error Message: CS0234: The type or namespace name 'Html' does not exist in the namespace 'System.Web.Mvc' (are you missing an assembly reference?)
编译器错误消息:CS0234:命名空间“System.Web.Mvc”中不存在类型或命名空间名称“Html”(您是否缺少程序集引用?)
Source Error:
Line 25: <add namespace="System.Web.Mvc" />
Line 26: <!--<add namespace="System.Web.Mvc.Ajax" />-->
Line 27: <add namespace="System.Web.Mvc.Html" />
Line 28: <add namespace="System.Web.Routing" />
Line 29: <add namespace="System.Web.WebPages" />
I have installed packets for the solution with NuGetand set up for all projects MVC3. Does MVC3include libraries System.Web.Mvc.Ajax, System.Web.Mvc.Htmland others? Why am I getting the error?
我已经为解决方案安装了数据包,NuGet并为所有项目进行了设置MVC3。不MVC3包括图书馆System.Web.Mvc.Ajax, System.Web.Mvc.Html和其他人呢?为什么我收到错误?
In References folder, I have System.Web.Mvc
在 References 文件夹中,我有 System.Web.Mvc
Runtime version: v4.0.30319,
Runtime version: v4.0.30319,
Version: 3.0.0.0
Web.config
网页配置
<?xml version="1.0" encoding="utf-8"?>
<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=152368
-->
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
<add key="webpages:Version" value="1.0.0.0" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<authentication mode="Forms">
<forms loginUrl="~/Account/LogOn" timeout="2880" />
</authentication>
<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.Routing" />
<add namespace="System.Web.WebPages" />
</namespaces>
</pages>
<httpRuntime targetFramework="4.5" encoderType="System.Web.Security.AntiXss.AntiXssEncoder, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<machineKey compatibilityMode="Framework45" />
<profile defaultProvider="DefaultProfileProvider">
<providers>
<add name="DefaultProfileProvider" type="System.Web.Providers.DefaultProfileProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" applicationName="/" />
</providers>
</profile>
<membership defaultProvider="DefaultMembershipProvider">
<providers>
<add name="DefaultMembershipProvider" type="System.Web.Providers.DefaultMembershipProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/" />
</providers>
</membership>
<roleManager defaultProvider="DefaultRoleProvider">
<providers>
<add name="DefaultRoleProvider" type="System.Web.Providers.DefaultRoleProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" applicationName="/" />
</providers>
</roleManager>
<sessionState mode="InProc" customProvider="DefaultSessionProvider">
<providers>
<add name="DefaultSessionProvider" type="System.Web.Providers.DefaultSessionStateProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" />
</providers>
</sessionState>
</system.web>
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
</system.webServer>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="3.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
</entityFramework>
<connectionStrings>
<add name="EFDbContext" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=SportStore;Integrated Security=True;Pooling=False;" providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
MSDNthe System.Web.Mvc namespace
MSDNSystem.Web.Mvc 命名空间
file packages.config:
文件packages.config:
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="EntityFramework" version="5.0.0-rc" />
<package id="jQuery" version="1.7.2" />
<package id="jQuery.UI.Combined" version="1.8.11" />
<package id="jQuery.Validation" version="1.9" />
<package id="Microsoft.AspNet.Mvc" version="3.0.20105.1" />
<package id="Microsoft.AspNet.Providers.Core" version="1.0" />
<package id="Microsoft.AspNet.Providers.LocalDB" version="1.0" />
<package id="Microsoft.AspNet.Razor" version="1.0.20105.408" />
<package id="Microsoft.AspNet.WebPages" version="1.0.20105.408" />
<package id="Microsoft.Web.Infrastructure" version="1.0.0.0" />
<package id="Modernizr" version="2.0.6" />
<package id="Moq" version="4.0.10827" />
<package id="Ninject" version="3.0.1.10" />
</packages>
采纳答案by Toffee
Clean your solution and then set the property of those files to Copy Local = True.
清理您的解决方案,然后将这些文件的属性设置为Copy Local = True.
To set the Copy Local property to True or False
将 Copy Local 属性设置为 True 或 False
In Solution Explorer, click the Show All Files button to display the References node.
在解决方案资源管理器中,单击“显示所有文件”按钮以显示“引用”节点。
- Open the References node for the project.
- Right-click a reference in the References list, and click Properties. The properties associated with that reference appear in a list in the Properties window.
- In the Properties window, change the Copy Local property to True or False.
- 打开项目的引用节点。
- 右键单击“引用”列表中的引用,然后单击“属性”。与该引用关联的属性出现在“属性”窗口的列表中。
- 在“属性”窗口中,将“复制本地”属性更改为 True 或 False。
回答by BlackSpy
This one normally catches me when I run from IIS and the app pool for the default site is set to .NET version 2.0. When using IIS from visual studio it creates a virtual directory but still runs under the default site's app pool. If using the build in web server, right click on your web project, go to properties and make sure you're running it under the right version of .NET. On IIS check the .NET version on your app pool.
当我从 IIS 运行并且默认站点的应用程序池设置为 .NET 版本 2.0 时,这通常会引起我的注意。从 Visual Studio 使用 IIS 时,它会创建一个虚拟目录,但仍会在默认站点的应用程序池下运行。如果使用内置 Web 服务器,请右键单击您的 Web 项目,转到属性并确保您在正确版本的 .NET 下运行它。在 IIS 上检查应用程序池上的 .NET 版本。
Following on from my last comment about how the project was created - are you correctly including the assemblies, as below (taken from the default web.config file generated by the MVC3 project template in VS10):
继我上次关于项目创建方式的评论之后 - 您是否正确包含了程序集,如下所示(取自 VS10 中的 MVC3 项目模板生成的默认 web.config 文件):
<compilation debug="true" targetFramework="4.0">
<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>
回答by Peter Munnings
I had the same problem - my scenario was that I was referencing the new System.Web.Mvc.dll from a lib folder and I did not have "Copy Local" set to true. The application was then reverting back to the version in the GAC which didn't have the correct namespaces (Html, Ajax etc) in it and was giving me the run time error.
我遇到了同样的问题 - 我的情况是我从 lib 文件夹中引用了新的 System.Web.Mvc.dll 并且我没有将“Copy Local”设置为 true。然后,应用程序恢复到 GAC 中没有正确命名空间(Html、Ajax 等)的版本,并给了我运行时错误。
回答by Sean
This answer is rather late given the question date, but someone may find it useful.
鉴于问题日期,这个答案相当晚,但有人可能会发现它很有用。
None of the above solutions worked for me in Visual Studio 2012 on a local project, Net Framework 4.5 MVC 4. For context, I was following a tutorial on creating a barebones Web Api on CodeProject (http://www.codeproject.com/Articles/615805/Creating-a-Clean-Minimal-Footprint-ASP-NET-WebAPI) when I ran into this problem.
在 Visual Studio 2012 的本地项目 Net Framework 4.5 MVC 4 中,上述解决方案均不适合我。对于上下文,我正在学习有关在 CodeProject 上创建准系统 Web Api 的教程(http://www.codeproject.com/文章/615805/Creating-a-Clean-Minimal-Footprint-ASP-NET-WebAPI)当我遇到这个问题时。
What did work for me was explicitly adding "System.Web.Mvc" to my application's references, even though the application already did reference "System.Web".
对我有用的是将“System.Web.Mvc”显式添加到我的应用程序的引用中,即使应用程序已经引用了“System.Web”。
回答by Chris
For whatever reason, TWO of my solutions have web projects that spontaneously uninstalled asp.net MVC somehow. I installed it from Nuget and now they both work again. This happened after a recent batch of windows updates that included .net framework updates for the version I was using (4.5.1).
无论出于何种原因,我的两个解决方案都有以某种方式自发卸载 asp.net MVC 的 Web 项目。我从 Nuget 安装了它,现在它们都可以正常工作了。这是在最近的一批 Windows 更新之后发生的,其中包括我使用的版本 (4.5.1) 的 .net 框架更新。
Edit: From the .Net Web Development and Tools Blog:
编辑:来自 .Net Web 开发和工具博客:
Microsoft Asp.Net MVC Security Update MS14-059 broke my build!
回答by azarc3
My solution was under Manage Nuget Packages for Solution...-- I had umpteen updates to do for quite a few packages.
我的解决方案是在Manage Nuget Packages for Solution... 下——我有很多包需要更新。
Let me back up a half step and say that I screwed myself over because I moved the solution and projects from one folder to another... so things were already out of whack compared to where the projects thought things out to be. Everything moved over just fine, but apparently Nuget becomes confused unless you use a different approach than I did.
让我退后半步说我把自己搞砸了,因为我将解决方案和项目从一个文件夹移动到另一个文件夹......所以与项目认为的事情相比,事情已经变得不正常了。一切都很好,但显然 Nuget 变得混乱,除非你使用与我不同的方法。
Back to the solution... I simply went to Manage Nuget Packages for Solution...>> Updates>> Microsoft and .NETand hit the Update All button.
回到解决方案...我只是转到管理解决方案的 Nuget 包...>>更新>> Microsoft 和 .NET并点击全部更新按钮。
Everything was back to normal and happy.
一切都恢复了正常和快乐。
回答by Tom Stickel
I downloaded an mvc 5 project and the offending issue pointing at
我下载了一个 mvc 5 项目和指向的违规问题
<add namespace="System.Web.Mvc.Ajax" />
With this error message:
出现此错误消息:
CS0234: The type or namespace name 'Ajax' does not exist in the namespace 'System.Web.Mvc' (are you missing an assembly reference?)
-- > in which I came to this webpage and tried a clean and a few things above etc...
--> 我来到这个网页并尝试了一个干净的和上面的一些东西等等......
What worked for me was just to
对我有用的只是为了
Manage Nuget and Update all the packages. Then it worked fine.
回答by Ramakrishna
if any of above not solves your problem just set properties of System.Web.Mvc.dll to copy local ture.
如果以上任何一项都不能解决您的问题,只需设置 System.Web.Mvc.dll 的属性来复制本地 ture。
it will solves
它会解决
回答by Matt
You need to update MVC.
您需要更新MVC。
- Go to Tools -> NuGet Package Manager -> Manage NuGet Packages for Solution
- Click on "Updates"
- Update "Microsoft ASP.NET MVC"
- Rebuild Solution
- 转到工具 -> NuGet 包管理器 -> 管理解决方案的 NuGet 包
- 点击“更新”
- 更新“Microsoft ASP.NET MVC”
- 重建解决方案
回答by Andrey
Copy Local = True was solve for one of my projects. But in another project, I get the same error, tried to set Copy Local = true, but it not solve my problem. Changing the Target framework from 4.5.1 to 4.5 in Project Properties Helped with this.
Copy Local = True 解决了我的一个项目。但是在另一个项目中,我遇到了同样的错误,试图设置 Copy Local = true,但它没有解决我的问题。在项目属性中将目标框架从 4.5.1 更改为 4.5 对此有所帮助。

