Archive for the ‘visualstudio’ Category

Migrating ASP.NET MVC 2 applications to ASP.NET MVC 3 Preview 1

ASP.NET MVC 3 Preview 1 has just been released! More info on ScottGu’s blog.

Download ASP.NET MVC 3 Preview 1!

I just finished up a preview of my ASP.NET MVC application conversion tool to support conversions from ASP.NET MVC 2 to ASP.NET MVC 3.

The previous version of the app that converted from ASP.NET MVC 1.0 to ASP.NET MVC 2 is available here.

image

Download

The app is a single executable: Download MvcAppConverter-MVC3Preview1.zip (256 KB).

Usage

The only requirement for this tool is that you have .NET Framework 4 on the machine. You do not need to have Visual Studio or ASP.NET MVC installed (unless you want to open your project!). Even though the tool performs an automatic backup of your solution it is recommended that you perform a manual backup of your solution as well.

  • To convert an ASP.NET MVC 2 project built with Visual Studio 2010 to an ASP.NET MVC 3 project in Visual Studio 2010 perform these steps:
    • Launch the converter
    • Select the solution
    • Click the “Convert” button
  • To convert an ASP.NET MVC 2 project built with Visual Studio 2008 to an ASP.NET MVC 2 project in Visual Studio 2010:
    • Open the project in Visual Studio 2010 to convert the solution and project file formats from VS2008 to VS2010
    • Upgrade the .NET Framework target version of each project to .NET Framework 4 because ASP.NET MVC 3 is compatible with only .NET Framework 4
    • Launch the converter
    • Select the solution
    • Click the “Convert” button

What it can do

  • Open up ASP.NET MVC 2 projects from Visual Studio 2010 (no other versions of ASP.NET MVC or Visual Studio are supported)
  • Create a full backup of your solution’s folder
  • For every VB or C# project that has a reference to System.Web.Mvc.dll it will (this includes ASP.NET MVC web application projects as well as ASP.NET MVC test projects):
    • Update references to ASP.NET MVC 2
  • For every VB or C# ASP.NET MVC Web Application it will:
    • Change the project type to an ASP.NET MVC 3 project
    • Update the root ~/web.config references to ASP.NET MVC 3
    • Update the root ~/web.config to have a binding redirect from ASP.NET MVC 2 to ASP.NET MVC 3
    • Update the ~/Views/web.config references to ASP.NET MVC 3
  • Add or update the JavaScript files (add jQuery, add jQuery.Validate, add Microsoft AJAX, add/update Microsoft MVC AJAX, add Microsoft MVC Validation adapter)
  • Unknown project types or project types that have nothing to do with ASP.NET MVC will not be updated

What it can’t do

  • It cannot convert projects build with Visual Studio 2008 or with .NET Framework 3.5.
  • It can have issues if your solution contains projects that are not located under the solution directory.
  • If you are using a source control system it might have problems overwriting files. It is recommended that before converting you check out all files from the source control system.
  • It cannot change code in the application that might need to be changed due to breaking changes between ASP.NET MVC 2 and ASP.NET MVC 3.
  • The release notes include manual steps to upgrade your application.

Feedback, Please!

If you need to convert a project to ASP.NET MVC 3 please try out this application and hopefully you’re good to go. If you spot any bugs or features that don’t work leave a comment here and I will try to address these issues in an updated release.

Other Resources

There are already a bunch of blog posts, walkthroughs, and even videos on the new preview, so check them out:

Brad Wilson has a four-part post on ASP.NET MVC 3’s new dependency injection support:

Creating Visual Studio projects that only contain static files

Have you ever wanted to create a Visual Studio project that only contained static files and didn’t contain any code?

While working on ASP.NET MVC we had a need for exactly this type of project. Most of the projects in the ASP.NET MVC solution contain code, such as managed code (C#), unit test libraries (C#), and Script# code for generating our JavaScript code.

MVC 2 Solution

However, one of the projects, MvcFuturesFiles, contains no code at all. It only contains static files that get copied to the build output folder:

MVC Futures Files project contents

As you may well know, adding static files to an existing Visual Studio project is easy. Just add the file to the project and in the property grid set its Build Action to “Content” and the Copy to Output Directory to “Copy if newer.” This works great if you have just a few static files that go along with other code that gets compiled into an executable (EXE, DLL, etc.).

But this solution does not work well if the projects only contains static files and has no compiled code. If you create a new project in Visual Studio and add static files to it you’ll still get an EXE or DLL copied to the output folder, despite not having any actual code. We wanted to avoid having a teeny little DLL generated in the output folder.

In ASP.NET MVC 2 we came up with a simple solution to this problem. We started out with a regular C# Class Library project but then edited the project file to alter how it gets built. The critical part to get this to work is to define the MSBuild targets for Build, Clean, and Rebuild to perform custom tasks instead of running the compiler. The Build, Clean, and Rebuild targets are the three main targets that Visual Studio requires in every project so that the normal UI functions properly. If they are not defined then running certain commands in Visual Studio’s Build menu will cause errors.

Once you create the class library projects there are a few easy steps to change it into a static file project:

The first step in editing the csproj file is to remove the reference to the Microsoft.CSharp.targets file because the project doesn’t contain any C# code:

    <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

The second step is to define the new Build, Clean, and Rebuild targets to delete and then copy the content files:

    <Target Name="Build">
        <Copy
            SourceFiles="@(Content)"
            DestinationFiles="@(Content->'$(OutputPath)%(RelativeDir)%(Filename)%(Extension)')" />
    </Target>
    <Target Name="Clean">
        <Exec Command="rd /s /q $(OutputPath)" Condition="Exists($(OutputPath))" />
    </Target>
    <Target Name="Rebuild" DependsOnTargets="Clean;Build">
    </Target>

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

The third and last step is to add all the files to the project as normal Content files (as you would do in any project type).

To see how we did this in the ASP.NET MVC 2 project you can download the source code and inspect the MvcFutureFules.csproj project file.

If you’re working on a project that contains many static files I hope this solution helps you out!