MVC and Web Forms: Two Great Tastes… Together?

The more I work with ASP.NET MVC, the more I can find good ways to use MVC together with Web Forms features

Not long ago on this site, I asked the question "Does ASP.NET MVC Let Me Stop Fighting Web Forms?" ( That article was born of years of what seems like nonstop fighting to get Web Forms to do what I needed, and how MVC seems to be the solution. Even if it means returning to page syntax (views) not all that cleaner than classic ASP.

I've not changed my mind about that—yet—but the more I work with MVC, the more I can find good ways to use MVC together with Web Forms features. This seems a bit strange, because when it first introduced ASP.NET MVC to the world Microsoft strongly implied that ne'er the twain shall meet. In other words, Web Forms and MVC are two different beasts. Choose one or the other, never both.

But it turns out that there are some compelling reasons to use the two together. So here I'm going to briefly explore some of the ways that using them together can make sense. And despite years of pain with Web Forms, maybe the two technologies really can and should coexist. We'll see!

Integrate MVC into an Existing Web Forms Application
Surprisingly enough, you can work new features into an existing Web Forms application with MVC. The application has to be a Web application rather than a Web site project, but that is a small hurdle to overcome. There are several things you need to do (not necessarily in this order):

  1. Modify the Visual Studio project file to include new GUIDs for MVC applications.
  2. Add references to the MVC assemblies (System.Web.Mvc, System.Web.Routing, and System.Web.Abstractions) to your project.
  3. Create folders for your Controllers and Views, along with any relevant subfolders, and, of course, the necessary code files.
  4. Modify the existing web.config file or files to add support sections as well as add a new web.config to the directory with the MVC portion of the application. This is the most complex step, but you can copy and paste sections of the configuration file from an MVC project.
  5. Modify the global.asax file to include the route definitions you want to use.

Once you make these modifications, you can add MVC elements to the project.

This option is not for the faint of heart. Stephen Walther's book (see list at the end of the article) goes into some detail, and Microsoft documents what is required. It's a nice, albeit somewhat complex, way to start introducing MVC to a large Web Forms application.

Add Web Forms Pages to an MVC Application
This is probably the easiest option of all. Since both Web Forms and MVC are built on top of the same default ASP.NET ViewEngine, you can simply add Web Forms pages to an MVC application, write them as you have for years, and run the page.

The only trick here is that the page can't mimic the default routing you define for the MVC application. If a user requests a page that doesn't match any route, ASP.NET will go looking for the actual page requested. On the other hand, you can take full advantage of routing for the Web Forms pages. Just be careful, because mixing and matching like this can make for some subtle and hard-to-find bugs in your site, particularly when dealing with default pages. Make sure that your route definitions don't collide with routes and controllers you set up in the MVC application. Otherwise you may never run the intended controller.

Session State and Management
You can hook into the various ASP.NET state management features reasonably well in an MVC application, along with some of the caching features. For session state, you can use the Session object much as you would in a Web Forms application. For testing, MVC wraps the Session object in HttpSessionStateBase object, which removes the dependency of the object on the HttpContext object. For cookies, you can use Response.Cookies as usual, as well as the HttpResponseBase for testing.

Use Web Forms Server Controls in an MVC Application
If you've written a lot of Web Forms applications and know the built-in server controls well, you may be happy to find out that you can use some of them in an MVC application. Not all of them, however. The problem is that some of the controls—too many, actually—make use of ViewState and postbacks, features that you probably use MVC to get away from. You also won't want to use the runat="server" attribute, nor use a server-side form tag. There are certainly some complications with this technique, so explore it before committing to use it in a production application.

The Palermo book I have listed at the end of the article goes into this option in some detail. But as they say in the book, the code they present is "exploratory…. Most of it contains hacks and other workarounds that go against the intended design of an MVC web application." So beware. But you can expect that Microsoft will make more of the server controls usable in MVC over time. And most of the third-party component vendors are releasing MVC control products, which integrate nicely with MVC rather than fighting against it.

For the most part, I've avoided this technique. I haven't yet found a compelling reason to force-fit these controls into MVC, and AJAX and jQuery present more palatable options when use with regular HTML tags. Use with caution.

For the most part, you can use ASP.NET personalization features in an MVC application. In a Web Forms application, you'd link your personalization properties to page controls to manage the data. But in an MVC application you need to make the link in a controller, adding each property to ViewData individually or the entire profile object all at once. This works nicely with testing as well.

MVC Books
As I've learned and worked with ASP.NET MVC, I've found that three of the earliest books on the topic are all great sources of information. If you're looking to learn MVC or learn it more deeply, you won't go wrong with any of these. All are about MVC 1.0, since 2.0 is still months away, and these books should still be good resources for learning MVC for a long while. The second two are written by Microsoft employees, so provide an insider's view and detail, while the first book is written by independent alpha geeks.

All three books contain varying discussions of using Web Forms and MVC together, along with many of the pitfalls. Combined, they provide a lot of information that will help you decide whether to explore using both technologies together.

ASP.NET MVC in Action, by Jeffrey Palermo, Ben Scheirman, and Jimmy Bogard. Manning, ISBN 1933988622.

ASP.NET MVC Framework Unleashed, by Stephen Walther. SAMS, ISBN 0672329980.

Professional ASP.NET MVC 1.0 by Rob Conery, Scott Hanselman, Phil Haack, and Scott Guthrie. Wrox, ISBN 0470384611.

Hide comments


  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.