简体   繁体   English

命名空间“System.Web.Mvc”中不存在类型或命名空间名称

[英]The type or namespace name does not exist in the namespace 'System.Web.Mvc'

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 NuGet and set up for all projects MVC3 .我已经使用NuGet为解决方案安装了数据包,并为所有项目MVC3 Does MVC3 include libraries System.Web.Mvc.Ajax , System.Web.Mvc.Html and others? MVC3是否包含System.Web.Mvc.AjaxSystem.Web.Mvc.Html等库? Why am I getting the error?为什么我收到错误?


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>

MSDN the System.Web.Mvc namespace MSDN System.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>

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。

For whatever reason, TWO of my solutions have web projects that spontaneously uninstalled asp.net MVC somehow.无论出于何种原因,我的两个解决方案都有以某种方式自发卸载 asp.net MVC 的 Web 项目。 I installed it from Nuget and now they both work again.我从 Nuget 安装了它,现在它们都可以正常工作了。 This happened after a recent batch of windows updates that included .net framework updates for the version I was using (4.5.1).这是在最近一批 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! Microsoft Asp.Net MVC 安全更新 MS14-059 破坏了我的构建!

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.当我从 IIS 运行并且默认站点的应用程序池设置为 .NET 版本 2.0 时,这通常会引起我的注意。 When using IIS from visual studio it creates a virtual directory but still runs under the default site's app pool.从 Visual Studio 使用 IIS 时,它会创建一个虚拟目录,但仍会在默认站点的应用程序池下运行。 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.如果使用内置 Web 服务器,请右键单击您的 Web 项目,转到属性并确保在正确版本的 .NET 下运行它。 On IIS check the .NET version on your app pool.在 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>

You need to update MVC.您需要更新MVC。

  1. Go to Tools -> NuGet Package Manager -> Manage NuGet Packages for Solution转到工具 -> NuGet 包管理器 -> 管理解决方案的 NuGet 包
  2. Click on "Updates"点击“更新”
  3. Update "Microsoft ASP.NET MVC"更新“Microsoft ASP.NET MVC”
  4. Rebuild Solution重建解决方案

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.我遇到了同样的问题 - 我的情况是我从 lib 文件夹中引用了新的 System.Web.Mvc.dll 并且我没有将“Copy Local”设置为 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.然后,应用程序恢复到 GAC 中没有正确命名空间(Html、Ajax 等)的版本,并给了我运行时错误。

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 .NET and hit the Update All button .回到解决方案...我只是转到管理解决方案的 Nuget 包... >>更新>> Microsoft 和 .NET并点击全部更新按钮

Everything was back to normal and happy.一切都恢复了正常和快乐。

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.

在 nuget 包管理器中运行以下命令为我修复了它:

update-package microsoft.aspnet.mvc -reinstall

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”。

I had the same issue, but none of the above worked for me.我有同样的问题,但以上都不适合我。 They did put me in to the right direction though.不过,他们确实让我朝着正确的方向前进。

For example when I set the "Copy Local" to "true" for System.Web.Mvc reference, it automatically sets it back to False.例如,当我将 System.Web.Mvc 引用的“Copy Local”设置为“true”时,它会自动将其设置回 False。

I have multiple projects which depend on the System.Web.Mvc reference in my solution, but only one caused this problem.我有多个项目依赖于我的解决方案中的 System.Web.Mvc 引用,但只有一个项目导致了这个问题。 In VS 2012 this reference is labeled with the yellow attention triangle.在 VS 2012 中,此参考标有黄色注意三角形。

Find this reference => remove it => re-add it找到此参考 => 删除它 => 重新添加

That fixed it for me.那为我修好了。 Hope this helps希望这可以帮助

View and check the reference paths in your csproj.查看并检查 csproj 中的参考路径。

I had removed references to System.Web.Mvc (and others) and readded them to a custom path.我已经删除了对 System.Web.Mvc(和其他人)的引用,并将它们读到了自定义路径中。 C:\\Project\\OurWebReferences

However, after doing this, the reference path in the still csproj did not change.但是,这样做之后,仍然csproj中的引用路径并没有改变。 WAS曾是

<Reference Include="System.Web.Mvc, Version=4.0.0.1, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
  <SpecificVersion>False</SpecificVersion>
  <HintPath>..\..\..\..\OurWebProject\bin\Debug\System.Web.Mvc.dll</HintPath>
</Reference>

Changed to manually改为手动

<Reference Include="System.Web.Mvc, Version=4.0.0.1, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
  <SpecificVersion>False</SpecificVersion>
        <HintPath>..\..\..\..\OurWebReferences\System.Web.Mvc.dll</HintPath>
</Reference>

Paths are an example only.路径只是一个例子。

对我来说,解决方案是确保所有项目都为同一个 CPU 构建——在我的例子中是 x86

Copy Local = True was solve for one of my projects. Copy Local = True 解决了我的一个项目。 But in another project, I get the same error, tried to set Copy Local = true, but it not solve my problem.但是在另一个项目中,我遇到了同样的错误,试图设置 Copy Local = true,但它没有解决我的问题。 Changing the Target framework from 4.5.1 to 4.5 in Project Properties Helped with this.在项目属性中将目标框架从 4.5.1 更改为 4.5 对此有所帮助。

I did something really stupid (and maybe you did too).我做了一些非常愚蠢的事情(也许你也做了)。

I was trying to call System.Web.Mvc.Html.Partial("<Partial Page>")我试图调用System.Web.Mvc.Html.Partial("<Partial Page>")

System.Web.Mvc.Html is a namespace and not a class and I didn't read my error message so well, so I interpreted my error as the class Html does not exist in the namespace System.Web.Mvc and that's how I ended up here (stupid I know). System.Web.Mvc.Html 是一个命名空间而不是一个类,我没有很好地阅读我的错误消息,所以我将我的错误解释为命名空间 System.Web.Mvc 中不存在类Html ,这就是我的方式最后到了这里(愚蠢的我知道)。

All I needed to do was add a using statement @using System.Web.Mvc.Html to my page and then @Html.Partial worked as expected.我需要做的就是在我的页面中添加一个 using 语句@using System.Web.Mvc.Html然后@Html.Partial按预期工作。

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它会解决

I had the same problem, but in my case was originated by other factor.我有同样的问题,但在我的情况下是由其他因素引起的。 I write this to help other people with the same problem.我写这篇文章是为了帮助其他有同样问题的人。 I have a solutions with multiple projects.我有多个项目的解决方案。 two of them work with the System.Web.Mvc, Normally our reference was pointing to external packages where we controlled the used version.其中两个与 System.Web.Mvc 一起使用,通常我们的引用指向我们控制使用版本的外部包。 For some reason one of the project reference goes to GAC and point to the GAC dll (version 4.0.0.1) and the program got the error.出于某种原因,项目引用之一转到 GAC 并指向 GAC dll(版本 4.0.0.1),程序出现错误。 To correct it:要纠正它:

Simple check if the references to System.Web.Mvc point to the same dll in the same directory path.简单检查对 System.Web.Mvc 的引用是否指向同一目录路径中的同一 dll。

I hope that help.我希望有帮助。

我已经从我网站的 Bin folder 中删除了 System.Web.dll。

I had this problem, setting copy local on and off etc did not work.我遇到了这个问题,设置复制本地打开和关闭等不起作用。 The project was not using nuget so fixes around that were out of the question also.该项目没有使用 nuget,因此修复也无法解决。

The fix for me was to install MVC 3 (which I did through the web platform installer).对我来说,修复方法是安装 MVC 3(我是通过 Web 平台安装程序完成的)。

I suspect this will become more of a solution for people as the .net framework continues to advance and people don't have the older MVC stuff installed.我怀疑随着 .net 框架的不断发展,人们不再安装旧的 MVC 东西,这将成为更多人的解决方案。

It's likely that setting your DLL to CopyLocal/true or any of the other major fixes for this will fix your issue, but here is another edge-case that caught me for 20 minutes of wasted time.很可能将您的 DLL 设置为 CopyLocal/true 或任何其他主要修复程序将解决您的问题,但这是另一个边缘情况,让我浪费了 20 分钟的时间。

When you add your namespaces to your Views/Web.config, ensure they are cased correctly:将命名空间添加到 Views/Web.config 时,请确保它们的大小写正确:

  <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="THE.NameSpace" />
        <add namespace="THE.Namespace" />
      </namespaces>
    </pages>
  </system.web.webPages.razor>

Ok so a lot of people seem to be experiencing this issue.好的,所以很多人似乎都遇到了这个问题。 My problem was caused by references having paths to the bin folders of other projects, or referencing dlls from system folders.我的问题是由于引用具有指向其他项目的 bin 文件夹的路径或从系统文件夹引用 dll 引起的。 The project was about 6 years old and the previous developers had decided that this was the way they were going to reference their libraries.该项目大约有 6 年历史,之前的开发人员已经决定这是他们引用库的方式。

The solution was to go through every reference and check the paths, then remove the reference and add it using NuGet.解决方案是遍历每个引用并检查路径,然后删除引用并使用 NuGet 添加它。 For any package that wasn't on NuGet I created another folder alongside packages and put the dll in there in the same layout as NuGet would.对于不在 NuGet 上的任何包,我在包旁边创建了另一个文件夹,并将 dll 放在与 NuGet 相同的布局中。

I also had to go through the config files and make sure the correct version of the packages was being used.我还必须检查配置文件并确保使用的是正确版本的软件包。 A long and painful process!一个漫长而痛苦的过程!

I have a project that does this whenever I build with the View open.我有一个项目,每当我在打开视图的情况下构建时都会执行此操作。 As soon as I closed the view, the error goes away and the build succeeds.一旦我关闭视图,错误就会消失并且构建成功。 Very strange.很奇怪。

我遇到了同样的问题,没有一个解决方案对我System.Web.MVC ,最后我删除了System.Web.MVC并再次添加然后一切恢复正常,我的问题解决了。

As so often happens, all of the radical steps in the other answers were unnecessary for me.正如经常发生的那样,其他答案中的所有激进步骤对我来说都是不必要的。 Instead, I had an error which wasn't being flagged correctly.相反,我遇到了一个没有被正确标记的错误。 My advice is to read the message before doing anything too drastic!我的建议是在做任何过于激烈的事情之前阅读消息!

The error said:错误说:

在此处输入图片说明

The file quoted was a temporary file - not much use there.引用的文件是一个临时文件 - 在那里没有多大用处。 But when I did a global search for the offending view model vmPayConfirm I found this:但是当我对有问题的视图模型vmPayConfirm进行全局搜索时,我发现了这个:

在此处输入图片说明

Once I corrected this error (I'd used the wrong namespace to reference the view model) everything worked perfectly!一旦我纠正了这个错误(我使用了错误的命名空间来引用视图模型),一切都完美无缺!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类型或名称空间名称“ Routing”在名称空间“ System.Web”中不存在 - The type or namespace name 'Routing' does not exist in the namespace 'System.Web' 类型或命名空间名称Web在命名空间系统中不存在 - Type or Namespace name Web does not exist in the namespace system 命名空间“System.Web”中不存在类型或命名空间名称“优化” - The type or namespace name 'Optimization' does not exist in the namespace 'System.Web' 类型或名称空间名称“ Http”在名称空间“ System.Web”中不存在 - The type or namespace name 'Http' does not exist in the namespace 'System.Web' 命名空间“System.Web”中不存在类型或命名空间名称“Mvc”(您是否缺少程序集引用?)Kentico - The type or namespace name 'Mvc' does not exist in the namespace 'System.Web' (are you missing an assembly reference?) Kentico Visual Studio:名称空间“ System.Web”中不存在类型或名称空间名称“ Mvc” - Visual Studio : The type or namespace name 'Mvc' does not exist in the namespace 'System.Web' 命名空间“System.Web”中不存在类型或命名空间名称“Mvc”(您是否缺少程序集引用?) - The type or namespace name 'Mvc' does not exist in the namespace 'System.Web' (are you missing an assembly reference?) 将解决方案从MVC 3.x升级到MVC 5.2.3:类型或名称空间名称“ Mvc”在名称空间“ System.Web”中不存在 - Upgrading solution from MVC 3.x to MVC 5.2.3: The type or namespace name 'Mvc' does not exist in the namespace 'System.Web' 命名空间system.web中不存在Mvc - Mvc does not exist in the namespace system.web 命名空间“System.Windows”中不存在类型或命名空间名称“部署” - The type or namespace name 'Deployment' does not exist in the namespace 'System.Windows'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM