Opening an MVC3 project in VS2012

2019-06-17 04:50发布

问题:

I am trying to open an MVC3 solution in a newly installed Visual Studio Express 2012 RC. So far, I've had issues that for some reason VS2012 doesn't know that the projects are MVC projects, so it won't add Views/Controllers. Got past this by adding the following GUID to the ProjectTypeGuids node of the .csproj file of each project in the solution:

{E53F8FEA-EAE0-44A6-8774-FFD645390401}

However, when I open razor views, it shows these errors (among other related ones):

Error    20    The name 'model' does not exist in the current context    c:\Users\willem\Documents\Visual Studio 2010\Projects\000-Orchard Development\src\Orchard.Web\Modules\EventManagement\Views\EditorTemplates\Parts\Event.cshtml    2    2    EventManagement

Error    21    The name 'T' does not exist in the current context    c:\Users\willem\Documents\Visual Studio 2010\Projects\000-Orchard Development\src\Orchard.Web\Modules\EventManagement\Views\EditorTemplates\Parts\Event.cshtml    5    14    EventManagement

Error    22    'System.Web.WebPages.Html.HtmlHelper' does not contain a definition for 'LabelFor' and no extension method 'LabelFor' accepting a first argument of type 'System.Web.WebPages.Html.HtmlHelper' could be found (are you missing a using directive or an assembly reference?)    c:\Users\willem\Documents\Visual Studio 2010\Projects\000-Orchard Development\src\Orchard.Web\Modules\EventManagement\Views\EditorTemplates\Parts\Event.cshtml    6    11    EventManagement

Intellisense is working in the razor view, but it only gives limited amount of fields for the Html helper method. For instance, none of the model specific methods like LabelFor and TextboxFor.

I have MVC 3 and 4 installed. The solution worked fine in VS2010.

UPDATE:

When adding a new MVC3 project in VS2012, it works fine. So that means it is definately something to do with the project, and not the installation.

UPDATE 2: I think that the issue is that the view doesn't have access to the referenced libraries in the root config:

<system.web.webPages.razor>
  <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
 <pages pageBaseType="Orchard.Mvc.ViewEngines.Razor.WebViewPage">
    <namespaces>
      <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" />
      <add namespace="System.Linq"/>
      <add namespace="System.Collections.Generic"/>
      <add namespace="Orchard.Mvc.Html"/>
    </namespaces>
  </pages>
</system.web.webPages.razor>

It does at runtime, but the intellilsense and error console doesn't pick it up

Any help is appreciated.

Thanks

回答1:

For me this problem was resolved by adding:

<add key="webpages:Version" value="1.0.0.0"/>

in my appSettings section of my root web.config

e.g.

 <appSettings>
     <add key="webpages:Version" value="1.0.0.0"/>
     <add key="ClientValidationEnabled" value="true"/>
     <add key="UnobtrusiveJavaScriptEnabled" value="true"/>
  </appSettings>


回答2:

Check this Link:

http://msdn.microsoft.com/en-us/library/hh266747(v=vs.110).aspx

Make sure you know that project is MVC3. Make sure that project was created with VS2010 with SP1.



回答3:

I fixed this by uninstalling MVC4 RC. Doesn't seem compatible with my system (or the 3 other systems I tested with). Will just have to wait for the release version of MVC4



回答4:

From MVC 4 release notes:

Required updates

  1. In the root Web.config file, add a new entry with the key webPages:Version and the value 1.0.0.0.

  2. In Solution Explorer, right-click the project name and then select Unload Project. Then right-click the name again and select Edit ProjectName.csproj.

  3. Locate the following assembly references: < Reference Include="System.Web.WebPages"/> < Reference Include="System.Web.Helpers" />

Replace them with the following:

<Reference Include="System.Web.WebPages, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL "/> 
<Reference Include="System.Web.Helpers, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" />

4.List item

Save the changes, close the project (.csproj) file you were editing, and then right-click the project and select Reload.



回答5:

You may install both MVC3 and MVC4 on the same computer, please configure in web.config like below

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