Adding MVC 3 features to an existing ASP.NET Web Forms application–Part 1

UPDATE: Scott Hanselman has a NuGet package to automate many of these steps and you should check it out, http://www.hanselman.com/blog/CreatingANuGetPackageIn7EasyStepsPlusUsingNuGetToIntegrateASPNETMVC3IntoExistingWebFormsApplications.aspx

Today, at Tech Ed India 2011, I delivered this session – Web Forms vs. MVC choosing the right one for web development. Although its a tricky topic, it was worth clarifying some of the patterns to decide between Web Forms and MVC. One of the demo scenarios I took up was using them in a hybrid scenario. Thanks to Scott Hunter from the ASP.NET Team, I got this dimension where we can use both together instead of re-writing the application from the scratch.

So, here below are the steps. For the purpose of this post, I am using Visual Studio 2010 Service Pack 1 and ASP.NET MVC 3 with Razor. But all of this can be reused for integrating MVC 2 based applications as well.

To begin with, we will create a Web Forms application (ASP.NET 4) using Visual Studio 2010. So, a File – New – Web Application and giving it a name HybridWebApp.

image

It would create the default web form templates. We would create a folder “Models” in the root directory where we would be adding our Entity Model. To begin, we will use a Database. For the purpose of this demo, I would be using the NorthWind sample database which can be downloaded from here

We will add an ADO.NET Entity Data Model into our application and give it a name NorthWindModel

image

Through the wizard, we connect to the NorthWind sample database and chose the tables “Products”, “Categories” and “Suppliers” and then complete the wizard to generate the ADO.NET Entity Model.

image

So once we rebuild the solution, we have the schema ready. Next, we can wire up this data into a web page using a GridView control. We will use the Default.aspx page created automatically to place the GridView and then using the GridView’s binding UI, try and bind to the TechEd Entity that we just created.

image

image

image

finally chose in the GridView the binding option to “Edit”, “Select” etc., and complete the wizard. With this our data binding with Web Forms is done and we have a page where we can see the Categories Listing.

image

Next, we want to add to this, an MVC Application. To do that we need to implement the following:-

1. Reference the MVC, Routing & WebPages assemblies in the Project

2. Reference the assemblies in the web.config file

3. Add the Routing mechanism to the Global.asax

4. Enable tooling support in Visual Studio for MVC (since this is a Web Form application)

5. Create the Controllers/Views for displaying data

So, first, we need to add Reference to the System.Web.Mvc which is installed typically in the C:\Program Files\Microsoft ASP.NET\ASP.NET MVC3\Assemblies folder. Also, we need to add Reference to the System.Web.Razor, System.Web.Helpers, System.Web.WebPages and the System.Web.WebPages.Razor DLLs located in the C:\Program Files (x86)\Microsoft ASP.NET\ASP.NET Web Pages\v1.0\Assemblies folder.

image

Next, in the Web.Config file, we will add the reference to the namespaces within the <system.web> tags

<pages>
<namespaces>
<add namespace="System.Web.Helpers" />
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
<add namespace="System.Web.WebPages"/>
</namespaces>
< /pages>

After that, we will add the assemblies’ references within the <compilation> tags

<compilation debug="true" targetFramework="4.0">
<assemblies>
<add assembly="System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add assembly="System.Web.Helpers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add assembly="System.Web.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add assembly="System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add assembly="System.Web.WebPages, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</assemblies>
</compilation>

The next step is to enable the Global.asax, we will see that in the Part 2 of this article.

Cheers!!!

Print | posted on Wednesday, March 23, 2011 9:55 PM

Comments on this post

# re: Adding MVC 3 features to an existing ASP.NET Web Forms application–Part 1

Requesting Gravatar...
this really very good article. but i want to do the reverse. I have ASP.NET MVC2 Project. Now i want to add some new module which is in ASP.NET webform. how to do it. need your guidance
Left by rambarat.saroj on May 06, 2011 11:48 AM

# re: Adding MVC 3 features to an existing ASP.NET Web Forms application–Part 1

Requesting Gravatar...
Finally I found everything I needed to write dissertations http://papersmart.net needs in this extremely detailed material of you. Surely I'll do my best to adopt your ideas properly and make a reasonable, skilled one. I hope that the audience can find ways on how to deal this issue right away.
Left by Mikplka on Jun 20, 2012 2:41 PM

# Awesome post

Requesting Gravatar...
Hey, bless you from my heart! I have been perusing your web journals from quite a while and I must like your great work. I have continually taken in a lot of people new things from you and might want to deliver such instructive post again and again pick up. web designing Delhi
Left by jeremywilliams on Mar 28, 2014 2:27 PM

# re: Adding MVC 3 features to an existing ASP.NET Web Forms application–Part 1

Requesting Gravatar...
I love the illustrations, makes it much easier to follow, as a general rule when I'm writing guides like help with windows restarting, I always make it a priority to include illustrations.
Left by Rodger on Dec 16, 2014 9:08 PM

# re: Adding MVC 3 features to an existing ASP.NET Web Forms application–Part 1

Requesting Gravatar...
This is the holy grail!
Been trying to add the same to my website from the past 1 month. Finally found the perfect solution. It was a bit difficult to implement these steps though but in the end, the efforts paid off. And the fact is that I have a Website Designing Company in Gurgaon, that's why it was extremely important for me to know this solution.
Great!!!
Left by Sam Cox on Mar 13, 2015 7:05 PM

# re: Adding MVC 3 features to an existing ASP.NET Web Forms application–Part 1

Requesting Gravatar...
Thanks for giving a clear idea on integrating MVC 3 features to ASP.NET Web Forms. It helped a lot.
Left by Cathrine on May 11, 2015 11:30 AM

# re: Adding MVC 3 features to an existing ASP.NET Web Forms application–Part 1

Requesting Gravatar...
Thanks for sharing a clear idea on integrating MVC 3 features to ASP.NET Web Forms. Thank You. Are you looking for Academic Writing help? Contact us: https://goo.gl/4fNrbq
Left by Jeremy Jones on May 11, 2015 11:39 AM

# re: Adding MVC 3 features to an existing ASP.NET Web Forms application–Part 1

Requesting Gravatar...
Learning to oversee business and personal financial portfolio is one outcome of a master's-level program in finance. Students could also learn to assess international markets and gain an understanding of tax structures. Focusing largely on intensive research, a doctoral degree program delves into the makeup of financial markets and teaches aspiring analysts and professors about security design and asset pricing. college essays help
Left by Josef on Jun 11, 2015 6:45 PM

# re: Adding MVC 3 features to an existing ASP.NET Web Forms application–Part 1

Requesting Gravatar...
very useful article helped me in my web designing
Left by Covalent Softwares on Feb 03, 2016 4:25 PM

# re: Adding MVC 3 features to an existing ASP.NET Web Forms application–Part 1

Requesting Gravatar...
Screenshots helps me to grab the idea quickly. The way you place MVC-3 features in ASP.net was brilliant. Essay writing services
Left by Larry on Feb 23, 2016 12:30 PM

# re: Adding MVC 3 features to an existing ASP.NET Web Forms application–Part 1

Requesting Gravatar...
ABOUT OPENING VARIOUS URL'S WITH THE SINGLE CLICK OF YOUR MOUSE BUTTON. THE FAST URL OPENER CAN HELP YOU ACHIEVE THIS GOAL AND SAVE YOU SOMETIME AND BOTHERATION OF CLICKING THE MOUSE KEYS AGAIN AND AGAIN, ENJOY THE WONDERS OF FAST URL OPENERAND SAVE TIME .
Left by gmod free no download on Apr 14, 2016 5:19 PM

# re: Adding MVC 3 features to an existing ASP.NET Web Forms application–Part 1

Requesting Gravatar...
I love the background. to me its more interesting than the girl chillin there. she picked a great backdrop, lots of character in there. Cotton Siro Slub Yarn
Left by hery on Apr 30, 2016 7:22 PM

# re: Adding MVC 3 features to an existing ASP.NET Web Forms application–Part 1

Requesting Gravatar...
some of the .dll files are missing. I am unable to convert this Indian Chat Room to an asp app.
Left by Bol De India on Aug 07, 2016 9:22 PM

# re: Adding MVC 3 features to an existing ASP.NET Web Forms application–Part 1

Requesting Gravatar...
Well,this is very interesting to learn about this feature of ASP.net. High Quality Blinds Coventry
Left by Calvin on Nov 25, 2016 1:44 PM

# re: Adding MVC 3 features to an existing ASP.NET Web Forms application–Part 1

Requesting Gravatar...
Screenshots causes me to get the thought rapidly. The way you put MVC-3 includes in ASP.net was splendid.professional writing help
Left by haroldlewis on Oct 27, 2017 11:55 AM

# re: Adding MVC 3 features to an existing ASP.NET Web Forms application–Part 1

Requesting Gravatar...
Excellent Post! You really try to post every little detail in which you have done awesome job. I am very happy to discover your website. I have a presentation next week, and I am on the look for such information. college essay writing tips
Left by jaybond on Oct 31, 2017 12:06 PM

# re: Adding MVC 3 features to an existing ASP.NET Web Forms application–Part 1

Requesting Gravatar...
Screenshots helps me to grab the concept quickly
Left by Gunjan Singh on Nov 11, 2017 5:09 PM

# re: Adding MVC 3 features to an existing ASP.NET Web Forms application–Part 1

Requesting Gravatar...
Nice way to represent the information. Through screenshot it is easily understand by the user.
Website Designing Company In India
Left by Web Click India on Nov 11, 2017 5:13 PM

# re: Adding MVC 3 features to an existing ASP.NET Web Forms application–Part 1

Requesting Gravatar...
indeed, I concur with you that these level headed discussions nearly achieved its utmost. best research paper writing service Canada There ought to be an assurance of vitality autonomy. My associates from are likewise worried about this issue.
Left by haroldlewis on Nov 14, 2017 6:08 PM

# re: Adding MVC 3 features to an existing ASP.NET Web Forms application–Part 1

Requesting Gravatar...
very very nice information
ivf cost in india





Left by vicky on Apr 26, 2018 1:56 PM

Your comment:

 (will show your gravatar)