ChatGPT解决这个技术问题 Extra ChatGPT

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:

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?)

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. Does MVC3 include libraries System.Web.Mvc.Ajax, System.Web.Mvc.Html and others? Why am I getting the error?

In References folder, I have System.Web.Mvc

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

file 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>
what is your web server? is this in iis?
Express IIS in Visual Studio 2012
what is your project's target set to?
Depends on who hits this question - this is all that is needed for some of us Manage Nuget Packages for Solution... >> Updates

C
Community

Clean your solution and then set the property of those files to Copy Local = True.

To set the Copy Local property to True or 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.


Awesome! This fixed it for me on an MVC4 project. It is kind of obvious when you think about it. I never looked on the deployed project to see if the DLLs were there.
Thanks! This helped me out too!
You're welcome. Sometimes complex problems are solved by simple solution. :)
It works! I just change only one reference => System.Web.Mvc
Took me a little while to figure out what "those files" meant. Go through the assembly references on every project in the solution, anything with "Microsoft ASP.NET\ASP.NET MVC 3\Assemblies" or the like in the path is part of the problem.
C
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).

Edit: From the .Net Web Development and Tools Blog:

Microsoft Asp.Net MVC Security Update MS14-059 broke my build!


This just happened to me. However, I updated my MVC packages instead of reinstalling them (mine were outdated anyway). Is there a source or reason why this happened?
This looks to be related to KB2994379 or KB2992080, depending on which version of MVC you're referencing (5.1 and 5.0 respectively). The update delivers a new version of System.Web.Mvc which I presume is introducing a versioning conflict. Updating your MVC package will fix the error. If you can't update for whatever reason, you'll need to set the System.Web.Mvc.dll to be copied locally.
I had the same problem. Update the package and set copy local = true. Thanks everyone!!!!
MVC Copy local = true +1.
Same happened to me. Had to set MVC to copy local = FALSE, rebuild, copy local = TRUE, rebuild. All good.
J
J.Olufsen

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.

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):

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

All projects set to use .NET Framework 4.5. Does it mean that .NET 4.5 cannot find System.Web.Mvc.Ajax and other classes?
Normally it means that it's set to run as 4.5 but is actually running as something else meaning it can't find System.Web.Mvc.Ajax in System.Web.Mvc.
As an aside, what is your System.Web.dll version? Reason I ask is that you said you added the assemblies from NuGet - did you not create a MVC3 web project?
M
Matt

You need to update MVC.

Go to Tools -> NuGet Package Manager -> Manage NuGet Packages for Solution Click on "Updates" Update "Microsoft ASP.NET MVC" Rebuild Solution


Using the NuGet Package is good however is can download a ton of other assembles which may not be needed, however this will fix it.
Had the same problem, have some bounty.
P
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.


a
azarc3

My solution was under Manage Nuget Packages for Solution... -- I had umpteen updates to do for quite a few packages.

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.

Back to the solution... I simply went to Manage Nuget Packages for Solution... >> Updates >> Microsoft and .NET and hit the Update All button.

Everything was back to normal and happy.


I posted same type solution having accidentally skipping your post +1 for you
This was the fix for me as well.
Note that you may also need to update JQuery and Bootstrap after doing this
Updating the mvc,razor,and webpages nuget packages worked for me on an MVC5 project.
T
Tom Stickel

I downloaded an mvc 5 project and the offending issue pointing at

<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.

Oh, I'll leave my posting, but I see that azarc3 is saying the same thing. upvoting him.
u
user1477388

Running the following command in the nuget package manager fixed it for me:

update-package microsoft.aspnet.mvc -reinstall

This needs to be upvoted about a thousand more times.
@Chazt3n Didn't work for me, but it's a good suggestion so + 1
I tried all of the other methods, and technically using Package manager, uninstalled and re installed and updated and what not. So I was pessimistic about this answer, but desperate. This worked somehow.
S
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.

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".


Thanks. This resolved my issue (althought it was with DevExpress.Web.Mvc)
+1. I am using MvcSiteMapProvider and since it does not work with the latest assemblies of MVC, I could not do a "Update All" for the Microsoft ASP.Net MVC packages through nuget. One more suggestion is you probably need to do a "CopyLocal=True" for this assembly.
Just needed to push debug web application to new environment. Needed to add "copy local" to the reference.
b
bohem.be

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.

I have multiple projects which depend on the System.Web.Mvc reference in my solution, but only one caused this problem. In VS 2012 this reference is labeled with the yellow attention triangle.

Find this reference => remove it => re-add it

That fixed it for me. Hope this helps


V
Valamas

View and check the reference paths in your csproj.

I had removed references to System.Web.Mvc (and others) and readded them to a custom path. C:\Project\OurWebReferences

However, after doing this, the reference path in the still csproj did not change. 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.


R
Rob Bowman

For me, the solution was to ensure all projects were building for the same CPU - in my case x86


A
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.


J
Jared Beach

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 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).

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.


Visual Studio 2015 will flag the using as not needed, but if you let it remove it, then you will get errors like: "The type or namespace name 'Helpers' does not exist in the namespace 'Microsoft.Web' (are you missing an assembly reference?)".
M
Mark Good

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.


R
Ramakrishna

if any of above not solves your problem just set properties of System.Web.Mvc.dll to copy local ture.

it will solves


f
freedeveloper

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. 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. To correct it:

Simple check if the references to System.Web.Mvc point to the same dll in the same directory path.

I hope that help.


a
andre719mv

I have deleted System.Web.dll from Bin frolder of my site.


im my case yes. I was switching to old branch without MVC. And System.Web.dll in Bin folder broke my bild.
4
4imble

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.

The fix for me was to install MVC 3 (which I did through the web platform installer).

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.


M
Moby's Stunt Double

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.

When you add your namespaces to your Views/Web.config, ensure they are cased correctly:

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

J
John

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. 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.

The solution was to go through every reference and check the paths, then remove the reference and add it using 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.

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!


S
Sirwan Afifi

I had the same problem, none of the solutions worked for me, Finally I removed the System.Web.MVC and added again Then everything was back to normal and my problem solved.


A
Andy Brown

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:

https://i.stack.imgur.com/H9vbh.png

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:

https://i.stack.imgur.com/OUguw.png

Once I corrected this error (I'd used the wrong namespace to reference the view model) everything worked perfectly!