Converting a ASP.NET MVC 3 Project to MVC 4 - asp.net-mvc

What steps should I take to safely upgrade an existing ASP.NET MVC 3 project to the latest ASP.NET MVC 4 Developer Preview?

The major change is to upgrade the relevant references to the latest version (4.0 /2.0). You need to update your web config files to upgrade the version of the following namespaces
System.Web.Mvc ( Change to 4.0.0.0)
System.Web.Webpages (Change to 2.0.0.0)
System.Web.Helpers (Change to 2.0.0.0)
System.Web.WebPages.Razor (Change to 2.0.0.0)
Also you need to update the root level web config file to have these appsettings entries
<appSettings>
<add key="webpages:Version" value="2.0.0.0" />
<add key="PreserveLoginUrl" value="true" />
</appSettings>
Then you need to update the DLLs referenced in the project. Remove the existing MVC3 dll reference and add MVC4 (use nuget package manager to do this)
This link handles all aspects of the conversion. Refer it as needed.

Follow the official steps posted here:
Upgrading an ASP.NET MVC 3 Project to ASP.NET MVC 4
Note: These steps cover the official release--not the developer preview.

Better yet, use the auto-update tool:
https://www.nuget.org/packages/UpgradeMvc3ToMvc4

Your best bet it's installing the MVC4 from the platform installer.
Then follow the steps on the official asp.net/mvc/mvc4 page. If you follow (only) the Shyju instructions It wouldn't work.
When you end with the official instructions, look on your Views directory, and you may find another web.config with another reference to the MVC assembly and several others for razor.
You also want to change the versions of this lines. Basically replace every 3.0.0.0 you find for 4.0.0.0 and 1.0.0.0 for 2.0.0.0
IF YOU DON'T MAKE THIS LAST CHANGE, YOU MAY END WITH THE ERROR ON THE FOLLOWING QUESTION
Look for included dll's and make sure they're for the correct versions.

Related

Missing reference to System.Web.Mvc in Asp.net Unit Test?

I recently made some unit tests for my Asp.NET MVC application, or I tried to, it keep telling me "The type 'System.Web.Mvc.Controller' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Web.Mvc, Version=5.1.0.0, Culture=neutral.' Then I tried to add a reference to the above mentioned, but was only able to choose version 4.0.0.0 and now it tells me that my project already uses version 5.1.0.0, which is a higher version. What to do?
Thanks a bunch!
MVC 5 comes in from a NuGet package. Just as it did with the main MVC web project in your solution. Install MVC via NuGet into your Test project, and you should be good to go.
An educated guess, but you might have obtained the newer version via a nuget package(some of the newer MVC project templates do this), and thus it is not installed in GAC. Use the nuget package manager for the MVC project to determine if this is the case, and if so then mystery solved.
In the test project you can add the same package, or use the addreference->Browse to browse into the nuget dependencies and poke around under the MVC project till you find the DLL(it will be in the file system somewhere under SolutionName/MvcProjectName/packages).

Using the latest source of ASP.NET MVC4 RC1 in a project

My machine has ASP.NET MVC3 and ASP.NET MVC4 beta1 installed.
I just donwloaded the latest source of ASP.NET MVC4 from http://aspnetwebstack.codeplex.com/SourceControl/list/changesets
How can I create a project in VS2010 that uses the assemblies created from this sourcecode?
You may take a look at the following blog post. It's about an older version of ASP.NET MVC but the same applies for the MVC 4.0 Beta.
So here are the steps:
Create a new ASP.NET MVC 4 project in VS
Remove the reference of the System.Web.Mvc assembly (or whatever assembly you will be trying to debug)
Add a reference to the assembly you have compiled
In your web.config make sure that you remove the corresponding <add assembly="System.Web.Mvc, ..."> (the one with the strong name) or it will use the assembly from the GAC instead of your custom built one.
I downloaded the source at http://aspnetwebstack.codeplex.com/ and used the instructions there and there http://aspnetwebstack.codeplex.com/documentation to get a compiling solution with working unittests.
I then created a new MVC4-Beta1 project in the solution. I removed references to
System.Web.Mvc.dll
System.Web.WebPages.dll
and added references to the corresponding projects in the solution.
I then changed web.configs in the main folder and view folder.
<add key="webpages:Version" value="2.0.0.0" />
was changed to
<add key="webpages:Version" value="3.0.0.0" />
I can now compile the project and debug into the MVC-source.

Issue with Razor MVC3

MVC 3 RC
I registered both System.Web.Razor and System.Web.WebPages.Razor and still have System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup not recognized in my View/web.Config
Intellisence for Razor is not working
What can be wrong with my configuration
Add the assembly "System.Web.WebPages.Razor" to the main Web.config of the application.
For some reason the "ASP.NET MVC 3 Web Application" project template included in the RC1 does not fill it.
<add assembly="System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
It's not obvious from your question or the comments if you've done it, but in order to have full support for Razor in your system you need to install the runtime and the tooling using the following installer: http://www.microsoft.com/downloads/en/details.aspx?FamilyID=a920ccee-1397-4feb-824a-2dfefee47d54
Just putting the MVC 3 runtime binaries in your application and making changes to web.config is not enough to make Razor IntelliSense work in Visual Studio.
The problem disappeared from the time I removed Resharper

Debugging ASP.NET MVC source?

Trying to debug into ASP.NET MVC 1.0 source, I followed instructions like these, basically remove reference to system.web.mvc from my web project and add the source project I downloaded instead.
Now, I have this problem,
The type 'System.Web.Mvc.FormMethod'
exists in both
'c:\Windows\assembly\GAC_MSIL\System.Web.Mvc\1.0.0.0__31bf3856ad364e35\System.Web.Mvc.dll' and in my AppData\Local\Temp\Temporary
ASP.NET
Files\root\dbcbb149\897fc019\assembly\dl3\796c00fb\f345f2d6_abe3c901\System.Web.Mvc.DLL'
I tried commenting out the following from web.config
<add assembly="System.Web.Mvc, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
but it'll give a difference error
The type 'System.Web.Mvc.Controller'
is defined in an assembly that is not
referenced. You must add a reference
to assembly 'System.Web.Mvc,
Version=1.0.0.0, Culture=neutral,
PublicKeyToken=31bf3856ad364e35'.
Could someone help on what's going wrong and how to fix it. Thank you very much!
Ray.
There is no need to uninstall ASP.NET MVC from GAC! (or any any <assemblyBinding>s) Just follow "Using the ASP.NET MVC source code to debug your app" article step by step.
There are a couple of questions similar to yours:
How can you use ASP.NET MVC build from source rather than the GAC?
MVC. Strongly-typed view difference (MVC sources vs. assembly)
Are the system.web.mvc assemblies in the GAC? you might need to remove them ... this of course could mess up future projects as the project templates will likely assume that they are in the gac.
You could also try using the <assemblyBinding> feature as described here to point the mvc stuff to the version that you are running.
Assuming the fully qualified assembly name for the one you're linking to differs from the one in the GAC, use the <QualifyAssembly> element and specify which assembly you're actually linking to. If not, change your local source to change the MVC assembly you're building to 1.0.0.1 so it is different.
Edit: Double check the howto you linked. It worked for me.
removed the system.web.mvc reference in my project.
Added the System.Web.Mvc project from source.
Comment out the System.Web.Mvc reference in my project's web.config.
modified the System.we.b.mvc's /Views/Web.Config as stated in the howto.
Ran flawlessly.
for the qualify assembly section you want something like this (we do it for SQLite assembly):
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<qualifyAssembly partialName="System.Data.SQLite" fullName="System.Data.SQLite, Version=1.0.60.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139" />
</assemblyBinding>
</runtime>
Have a look at this article for the complete guide on how to setup MVC source.
There are two web.config files you need to update. One is in the MVC project folder and one is in the Views folder. It's likely you forgot to update the latter. Otherwise following the aforementioned article should do it.
A slightly more comprehensive version of Steve Sandersons instructions can be found here.
It explains how you can also include MVC Futures in your debugging.

Using asp.net mvc 2 features with the spark view engine

I am working with an ASP.NET MVC project which was originally started from the CodeBetter.Canvas project - and I'm trying to move to ASP.NET MVC 2. I successfully upgraded my project using Eilon's upgrade tool, moved to VS2010 (although not yet to .NET 4).
The issue I'm having currently is only occurring when using the spark view engine. Here is the relevant bit of code in my View.spark (strongly typed):
${Html.EditorFor(e => e)}
The same bit of code works just fine if I use an .aspx view:
<%= Html.EditorFor(e => e) %>
The major point here being "EditorFor" is new in ASP.NET MVC 2 and in my project I can use that helper in an ASPX view but not a Spark view.
I've tried upgrading Spark to use MVC 2 (as well as MvcContrib and Ninject), thinking maybe it was one of those that was freaking out - but so far no luck - I'm still seeing the same behavior.
Here is the full error message that is thrown from within Spark's BatchCompiler class.
Dynamic view compilation failed.
(0,0): warning CS1701: Assuming
assembly reference 'System.Web.Mvc,
Version=1.0.0.0, Culture=neutral,
PublicKeyToken=31bf3856ad364e35'
matches 'System.Web.Mvc,
Version=2.0.0.0, Culture=neutral,
PublicKeyToken=31bf3856ad364e35', you
may need to supply runtime policy
c:\inetpub\wwwroot[myproject]\CodeBetter.Canvas.Web\Views[MyEntity]\View.spark(9,16):
error CS1061:
'System.Web.Mvc.HtmlHelper' does not
contain a definition for 'EditorFor'
and no extension method 'EditorFor'
accepting a first argument of type
'System.Web.Mvc.HtmlHelper' could be
found (are you missing a using
directive or an assembly reference?)
Here is the Spark related code in my Global.asax:
var settings = new SparkSettings()
.AddNamespace("System")
.AddNamespace("System.Collections.Generic")
.AddNamespace("System.Web.Mvc")
.AddNamespace("System.Web.Mvc.Html")
.AddNamespace("MvcContrib.FluentHtml")
.AddNamespace("CodeBetter.Canvas")
.AddNamespace("CodeBetter.Canvas.Web")
.SetPageBaseType("ApplicationViewPage")
.SetAutomaticEncoding(true);
#if DEBUG
settings.SetDebug(true);
#endif
var viewFactory = new SparkViewFactory(settings);
ViewEngines.Engines.Add(viewFactory);
Also, I am referencing System.Web.Mvc.Html in my spark view as mentioned in another SO answer.
<use namespace="System.Web.Mvc.Html" />
The underlying issue is that a version 1.0.0 assembly is still being referenced somewhere in the project.
Using reflector to examine each assemblies dependencies, I found (if you're starting with CodeBetter.Canvas project) there are three dependent projects that need to be updated to use the 2.0.0 version of system.web.mvc.dll
Spark. Download the latest version
of spark from teamcity and use the
spark.web.mvc2.dll (which references
system.web.mvc.dll 2.0.0) Link to detailed instructions.
MVCContrib. Download the latest version
of MVCContrib (download release, download source) which references the
system.web.mvc.dll 2.0.0.
Ninject. Download the latest version of
ninject and recompile the VS solution
after updating the reference to the 2.0.0
version of system.web.mvc.dll
Now, replace these dependency's assemblies in your project and update the project references if necessary. All should be well.
I recently converted my spark project to asp.net mvc 2. I dont see in what you've written that you've switched from using Spark.Web.Mvc to Spark.Web.Mvc2.
Just to be clear, Spark.Web.Mvc2 is the project that needs to be recompiled with the new System.Web.Mvc assembly.
I had issues with intellisense until i removed all assembly and namespace references from the web.config and put them in the global.spark file and had added the pageBaseType="Spark.Web.Mvc2.SparkView" attribute to the Spark->Pages node in the web.config.
Hope any of that helps.
When you downloaded newest Spark sources, did you rebuild it using MVC 2 assemblies? Did you replace references in Spark project?
Try specifiying the System.Web.Mvc reference in the web.config instead of the Global.ascx.cs file so that you can specify the specific version:
<spark>
<compilation debug="true" defaultLanguage="CSharp">
<assemblies>
<add assembly="System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
</assemblies>
</compilation>
</spark>
This should prevent SPARK from working at all if the wrong version of the MVC assembly is being loaded from somewhere.
If the MVC 1.0 Assembly is in the GAC you can clear and reload it as follows:
(from Visual Studio command prompt)
ngen /delete System.Web.Mvc
ngen /delete System.Web.Abstractions
ngen update
Hope this helps,
Jeff French
These are the steps I took to resolve the issue with a new MVC 2 project and Spark 1.1:
Compile against MVC 2.0 - I double checked the references to make sure I was linking to MVC 2 and not MVC 1. Since this was a new project, this was not an issue.
Added System.Web.Mvc.Html - I added System.Web.Mvc.Html to the Spark configuration, to make sure that namespace was added to all views.
In Global.asax.cs Application_Start
var settings = new SparkSettings()
.SetDebug(true)
.SetAutomaticEncoding(true)
.AddAssembly("Web")
.AddNamespace("Web.Model")
.AddNamespace("System.Collections.Generic")
.AddNamespace("System.Linq")
.AddNamespace("System.Web.Mvc")
.AddNamespace("System.Web.Mvc.Html");
This can also be done in the webconfig in the Spark View Engine block.
Add the Typed Model - Make sure you type the Spark View Model. In aspx this is done with the Inherits in the page declaration, like this:
<%# Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master"
Inherits="System.Web.Mvc.ViewPage<MyModelType>" %>
in Spark:
<viewdata model="MyModelType" />
I tried a lot of things mentioned above but just couldn't get a dependent dll that my views used (and also referenced MVC 1.0.0.0) to use the MVC 2.0.0.0, so here is what fixed it for me...
I downloaded the Spark code and added the following line to the BatchCompiler.cs file in the Compile method:
compilerParameters.CompilerOptions = "/nowarn:1701";

Resources