The type or namespace name 'Mvc' does not

2019-06-16 06:18发布

After converting a Hybrid ASP.NET MVC1 app to MVC2 I'm getting the following error when I try and run the application:

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

The allegeded culprit in the web.config file is System.Web.Mvc:

<namespaces>
    <add namespace="System.Web.Mvc"/>
    <add namespace="System.Web.Mvc.Ajax"/>
    <add namespace="System.Web.Mvc.Html"/>

So far my investigation seems to lead me to believe that version 2 of System.Web.Mvc is not installed or has not been picked up.

I've tried creating a File > New Project based on MVC 2 and that's picking up the new (v2) version of MVC. I've also converted some other projects (that were not hybrids) and they've converted without problem to MVC2.

I've also uninstalled MVC1 to try and remove references to it from the GAC. However, none of this has worked.

Any ideas?

3条回答
唯我独甜
2楼-- · 2019-06-16 06:24

Make sure you have the binding redirect in your Web.config:

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

This forces MVC 2 even if MVC 1 is on the machine.

Also: MVC 1 does have a System.Web.Mvc namespace, so make sure you also have:

  <assemblies>
    <add assembly="System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
查看更多
\"骚年 ilove
3楼-- · 2019-06-16 06:32

I came across somthing similar and the solution was to change the reference to Microsoft.Web.Mvc so Copy Local was set to True.

查看更多
家丑人穷心不美
4楼-- · 2019-06-16 06:35

I had the same error and clicking the detailed compilation error link in the page showed problems with System.Web.Helpers, .Http, and .WebPages.

After backing up the system I ran update-package -reinstall' and forced an uninstall/reinstall of all packages. This forced the web.config file to be rebuilt correctly and the dependentAssembly sections were constructed right.

After rebuilding the solution it ran first time.

Note, it did add the following bad providers section to the web.config.

<contexts>
  <context type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</contexts>
<providers>
  <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>

This only generates a build warning, but I did remove it in favor of the preceeding contexts element.

查看更多
登录 后发表回答