October 2009 - Posts

Migrating ASP.NET MVC 1.0 applications to ASP.NET MVC 2

NOTE: There is an updated version of this tool that is compatible with ASP.NET MVC 2 RTM.

To help our customers adopt ASP.NET MVC 2 I built an application that helps upgrade Visual Studio 2008 solutions that use ASP.NET MVC 1.0 to use ASP.NET MVC 2 Preview 2. This application is inspired by Scott Hanselman’s blog post on updating ASP.NET MVC projects to work in Visual Studio 2010 Beta 1.

image

Download

The app is a single executable: Download MvcAppConverter.zip (220 KB).

Usage

The only requirement for this tool is that you have .NET Framework 3.5 SP1 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 the solution as well.

  • To convert an ASP.NET MVC 1.0 project built with Visual Studio 2008 to an ASP.NET MVC 2 project in Visual Studio 2008 perform these steps:
    • Launch the converter
    • Select the solution
    • Click the “Convert” button
  • To convert an ASP.NET MVC 1.0 project built with Visual Studio 2008 to an ASP.NET MVC 2 project in Visual Studio 2010:
    • Perform the above steps, then open the project in Visual Studio 2010 and it will perform the remaining conversion steps

What it can do

  • Open up ASP.NET MVC 1.0 projects from Visual Studio 2008 (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
    • Add a reference to System.ComponentModel.DataAnnotations 3.5 (if not already present)
  • For every VB or C# ASP.NET MVC Web Application it will:
    • Change the project type to an ASP.NET MVC 2 project
    • Update the root ~/web.config references to ASP.NET MVC 2
    • Update the root ~/web.config to have a binding redirect from ASP.NET MVC 1.0 to ASP.NET MVC 2
    • Update the ~/Views/web.config references to ASP.NET MVC 2
    • 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 directly to Visual Studio 2010 or to .NET Framework 4.
  • 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 1.0 and ASP.NET MVC 2. Consult the readme for information on breaking changes.

Feedback, Please!

If you need to convert a project to ASP.NET MVC 2 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.

Posted by Eilon with 39 comment(s)
Filed under: , ,

Introducing SmartyRoute: A smarty-ier way to do routing in ASP.NET applications

Inspired by ideas I have heard from people on my team (ideas that apparently I misunderstood!), I am introducing the new SmartyRoute, which provides a smarty-ier way to do routing in ASP.NET applications.

The basic idea is that you can make a request to a resource in an ASP.NET application without specifying a file extension in the URL. SmartyRoute will try to append a set of supported file extensions to find a handler to serve the request.

Basic Routing

In the simplest usage a user can make a request to ~/helloworld and SmartyRoute will find ~/helloworld.aspx on disk and use that as the handler. This is a very common scenario: Many developers don’t want to have file extensions in the URLs of their web sites. SmartyRoute enables developers to do that without making other changes to the application.

Slightly More Advanced Routing

A more advanced scenario is where a developer wants to have a URL such as ~/category/beverages. With the feature shown so far the developer has to have a ~/category/beverages.aspx on disk. There would also need to be another ASPX for every category in the application. However, SmartyRoute can walk up the path segments of the URL to locate a parent ASPX file. In this case SmartyRoute will detect that there is a file ~/category.aspx and load that instead. The rest of the data on the URL (in this case "beverages") can get passed in as parameters to the ASPX page.

Even More Advanced Routing

To get parameter values there are a number of helper APIs that SmartyRoute offers. The simplest way is to call SmartyRoute.GetRouteMatch().PathInfo, which will return everything after the last "/" of what was handled in the request. For example, if the user requests ~/category/beverages and it gets handled by ~/category.aspx then the remaining "beverages" value in the URL will be the path info:

    string category = SmartyRoute.GetRouteMatch().PathInfo; // category == "beverages" 

If the path info contains multiple segments there is an alternative API that will do some of the heavier lifting for the developer. For example, if the URL is ~/category/beverages/5 (where "5" is the page number) there are these helpers:

    string category = SmartyRoute.GetNextRouteValue(); // category == "beverages"
    int page = SmartyRoute.GetNextRouteValue<int>(); // page == 5

Setting It Up

To use SmartyRoute in an application add a reference to the eStuff.Routing project and then go to your global.asax.cs and add this simple registration:

    string[] supportedExtensions = new[] { "aspx", "ashx" };
    RouteTable.Routes.Add(new SmartyRoute(supportedExtensions));

And that’s it!

Sample Project

In the attached sample project (13KB) there is the eStuff.Routing project and a sample ASP.NET Web Application Project. Open the solution in Visual Studio 2008 (with SP1) and try out these URLs:

  1. http://localhost:59519/Default (will load ~/default.aspx)
  2. http://localhost:59519/foo/bar (will load ~/foo/bar.aspx)
  3. http://localhost:59519/category/beverages/5/soda (will load ~/category.aspx with parameters)
  4. http://localhost:59519/MyHandlers/Boring (will load ~/MyHandlers/Boring.ashx handler)
  5. http://localhost:59519/MyHandlers/CoolStuff/Quote (will try to load ~/MyHandlers/CoolStuff/Quote.ashx handler but will fail due to web.config security)

Note that you can still make requests using the file extensions: SmartyRoute doesn’t block any current functionality (not really, anyway).

Feedback, Please!

I’d love to hear whether people find this sort of routing functionality useful. The key advantage of this simplified routing system is that you, the developer, don’t have to specify any URL route formats, yet you get most of the advantages of ASP.NET Routing: Extensionless URLs, custom data in the path info, better SEO.

Would you use this in an application that you are building?

Are you already doing something similar in an existing application?

Posted by Eilon with 8 comment(s)
Filed under: , ,
More Posts