Integrating ASP.NET MVC into an existing ASP.NET Webforms application

If, like me you’re not always blessed with the opportunity to build every application from scratch, you may find yourself wanting to introduce the wholesome goodness of ASP.NET MVC into an existing ‘classic’ ASP.NET Webforms application. Most tutorials out there concentrate on nice green field development.

What follows is largely a reference for me to remember how to do this.¬† It’s basically a matter of manually injecting what the project templates do for a new application.¬† I’m also not professing to have come up with all of these steps – I’m just bringing them together.

I’ll assume you’ve got all the necessary prerequisites (MVC 4.0) installed already, and if you have a web ‘site’ project, then I’d suggest you update it to a web application before doing all of this.

Getting the structure and configuration to look like MVC

There’s a number of standard folders, and bits of code you’ll find dotted around MVC applications – Models/Views/Controller for instance ūüôā

The following article goes through the first steps of getting those folders into your project (assuming you don’t have a naming conflict).

Mixing ASP.NET WebForms and ASP.NET MVC

Updating to MVC4

This is all good, but that article’s a bit old, and you’ll find the next one brings you (mostly) up to date with MVC 4.0

Adding MVC 4.0 to WebForms Project

If you want to use any of the newer features such as bundling, or if you’ve copied some views from an MVC4 project into your new MVC project, you’ll need the ASP.NET Web Optimization Framework (get it from NuGet).

You may also want to take an MVC4 project and convert the global.asax code to call off to the classes in the App_Start folder…

    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            AuthConfig.RegisterAuth();
        }
    }

Getting the Visual Studio MVC Template Goodness

This is great, but the icing on the cake is to make Visual Studio think this is an MVC project, so you get the nice right-click options, like add–>Controller if you’re in the controllers folder.¬† It turns out you just need to fool Visual Studio by adding a project type guid in your web projects csproj file…

With Visual Studio I just did a quick diff between a new MVC 4 project’s project file, and my ‘hybrid’ project’s file.

The following is what you’re looking for…


<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{18BBC72C-702B-40CD-B347-D7FC66D276FA}</ProjectGuid>
<ProjectTypeGuids>{E3E379DF-F4C6-4180-9B81-6769533ABE47};{349c5851-65df-11da-9384-00065b846f21};{fae04ec0-301f-11d3-bf4b-00c04f79efbc}</ProjectTypeGuids>

If you just add the first ‘ProjectTypeGuids’ guid to the corresponding place in your project file, and reload, the magic happens, and Visual Studio thinks it’s now an MVC project.¬† You’ll probably find you already had the other 2 guids.

Book Review : Trojan Horse by Mark Russinovich

It’s been a while since I read Mark’s last book – Zero Day, and I finally got a copy of the sequel; Trojan Horse.

Like any followup action story, the main challenges for Russinovich here were to develop the main characters, introduce new characters, angles and plotlines, and also develop/increase the action. ¬†I’m glad to report all of these were successfully undertaken, and Trojan Horse kept me as entertained as Zero Day.

In this story we see Jeff Aitken and his partner (in more ways than one), Darryl Haugen get involved in another global cyber-crisis, with even more tangled international players than before, and even more audacious technical threats employed. ¬†Again, they find themselves getting rather closer to the action than they planned, and their simple ‘computer consultant’ existence is transformed into cyber-agent once again.

The action centres around the Iranian nuclear program, and all the interested players in its outcome.  International governments and secret agents galore, keep things moving pretty quickly, and the book is again organised in a way to make you piece together disparate events yourself to reveal the main plot.

Probably the most significant character development in the book was Darryl as she gains new strength through some traumatic events.  Jeff continues to excel from the technical side, whilst showing more reality, being rather more bumbling in other areas, like driving a manual car.

Talking of reality, it could be said that a few areas are somewhat Bond’esque in the licence they take from coincidence, good luck, and timing. ¬†I’m a big Bond fan, so I actually don’t mind if he escapes several times in a completely implausible fashion. ¬†I guess it all adds to the action – and again, would look great on screen.

I actually found it hard to put down as early as around half-way through, as it seemed things were slowly but significantly building to the climax from there.

The action is solid and the technical detail as always is second-to-none.  Jeff dabbles in other areas, such as Android in this story, so I do wonder quite how many languages he knows and areas of specialism he has!

As an IT guy too, I still find it hard to believe that Jeff can maintain his physique with the hours he works. ¬†Darryl is obviously good for him ūüôā

Looking forward to the next one.