Thursday 24 September 2009

Migrating Classic ASP.NET to ASP.NET MVC 2 – Project

I always laugh when I see ASP.NET referenced as Classic ASP.NET after I was exposed to “Classic” asp for a little while last year.  I mean no insult to anyone, it’s still a very relevant and popular framework, but it makes it easier for me to differentiate it in my blog.

This post by Maarten Balliauw is old, but still very relevant on how to migrate ASP.NET to ASP.NET MVC.  It was put together during the beta of version 1 so there are a few things missing, but it does include 95% of what you need to do.  I don’t want to re-cover someone else’s work, so I’ll assume you’ve started there before you found this page.

Extra Bits

One thing the article leaves out is that you won’t have the ability to right click your Controllers folder to add a new controller.  While this doesn’t really stop you from adding controllers as they are just classes, it does cause you to spend extra time writing the wrapper information and leaves the possibility of error more open.  Fortunately this is easy to enable.  In the project file (aspmvcwebsite.csproj or whatever) you’ll find this line in bold:

   1: <PropertyGroup>
   2:     <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
   3:     <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
   4:     <ProductVersion>9.0.30729</ProductVersion>
   5:     <SchemaVersion>2.0</SchemaVersion>
   6:     <ProjectGuid>{9383F928-27DF-4A63-B30B-F79FF733B034}</ProjectGuid>
   7:     <ProjectTypeGuids>{349c5851-65df-11da-9384-00065b846f21};{fae04ec0-301f-11d3-bf4b-00c04f79efbc}</ProjectTypeGuids>
   8:     <OutputType>Library</OutputType>
   9:     <AppDesignerFolder>Properties</AppDesignerFolder>
  10:     <RootNamespace>AdminWebUI</RootNamespace>
  11:     <AssemblyName>AdminWebUI</AssemblyName>
  12:     <TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
  13: </PropertyGroup>

It won’t, of course, be in bold in the csproj file, the ProjectTypeGuids define the flavour of project you chose when you started the project and it’s what the MVC framework looks for to setup your context sensitive menu.  All you need to do is add another flavour so your project type looks like an ASP.NET MVC project type.  If you create an ASP.NET MVC project and check out the csproj file you’ll see the extra guid you need, I’m not sure if it’s all the same for all flavours of MVC but mine was:

   1: {F85E285D-A4E0-4152-9332-AB1D724D3325};

Add this in first and you’ll have

   1: <ProjectTypeGuids>{F85E285D-A4E0-4152-9332-AB1D724D3325};{349c5851-65df-11da-9384-00065b846f21};{fae04ec0-301f-11d3-bf4b-00c04f79efbc}</ProjectTypeGuids>

Reload your project and you’ll have ASP.NET MVC context sensitive menus.  You can still add web forms and whatever else you like throughout your project.

View Web.config

If you’re going to have strongly typed views, very important for ASP.NET MVC, then you’ll also need the Web.config file in your Views folder.  I would suggest creating a new ASP.NET MVC project and then copying the file into the folder in your migration application.

Conclusion

That’s pretty much all you need.  You can run the two side by side with very little effort.  If you have any other issues, just compare the web.config files from a new ASP.NET MVC site with the config file that you already have and move the differences.  ASP.NET MVC 2 has this for example:

<dependentAssembly>
    <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35"/>
    <bindingRedirect oldVersion="1.0.0.0" newVersion="2.0.0.0"/>
</dependentAssembly>

That you’ll need when it gets to IIS, but is not needed locally. 

 

Hopefully someone finds this useful.

2 comments:

Anonymous said...

Thank you. This helped me a lot. It also works with MVC3.

You need to set the ProjectTypeGuids following in the .csproj file:

{E53F8FEA-EAE0-44A6-8774-FFD645390401};{349c5851-65df-11da-9384-00065b846f21};{fae04ec0-301f-11d3-bf4b-00c04f79efbc}

ASP.Net Migration said...

Nice information about migration.