ASP.NET 4.0 Hosting | How to Integrate ASP.NET MVC 3 into existing upgraded ASP.NET 4 Web Forms applications

ASP.NET Web Forms is a part of the ASP.NET web application framework and is included with Visual Studio. It is one of the four programming models you can use to create ASP.NET 4.0 web applications, the others are ASP.NET MVC, ASP.NET MVC 3, ASP.NET Web Pages, and ASP.NET Single Page Applications. In ASP.NET, you can have ASP.NET applications that are both WebForms and MVC (as well as WCF and ASMX Web Services and on and on.) While the File|New Project dialog gives you a choice between This and That, in fact it’s all ASP.NET underneath. You are welcome to mix and match “cafeteria style” and create apps in any combination you’d like.

The simple trick to add ASP.NET MVC 3 to an upgraded ASP.NET 2.0 WebForms application is :

  • Run the Upgrade Wizard (open the  Visual Studio2008 Web Application in Visual Studio 2010)
  • Create a default ASP.NET MVC application for reference (you’ll throw it away later)
  • Use a differencing tool like Beyond Compare to integrate the new web.config entries from the ASP.NET MVC sections into the upgraded ASP.NET WebForms application
  • Ok

This is more detailed for the description above :

UPGRADING AN ASP.NET 2.0 WEBFORMS APPLICATION

This is a simple Visual Studio 2008 ASP.NET WebForms Application running under .NET 2.

Integrating ASP.NET MVC 3 into existing upgraded ASP.NET 4 Web Forms applications 1

Now, open this application in Visual Studio 2010. You’ll get the Conversion/Upgrade Wizard.

Integrating ASP.NET MVC 3 into existing upgraded ASP.NET 4 Web Forms applications 2

Click Next >> Next >> Finish. You’ll get an prompt to upgrade the 2.0 application to .NET Framework 4. Click Yes.

Integrating ASP.NET MVC 3 into existing upgraded ASP.NET 4 Web Forms applications 3

Now upgraded it in Visual Studio 2010. It will runs and it’s still WebForms.

Integrating ASP.NET MVC 3 into existing upgraded ASP.NET 4 Web Forms applications 4

Please take a new default ASP.NET MVC 3 application with Beyond Compare and compare the upgraded app with the default app.

Integrating ASP.NET MVC 3 into existing upgraded ASP.NET 4 Web Forms applications 5

Please copy over these folders and files:

  • Content
  • Controllers
  • Models
  • Scripts
  • Views
  • Global.asax, Global.asax.cs

This is the new references from the upgraded application. The old on the left and the new on the right.

Integrating ASP.NET MVC 3 into existing upgraded ASP.NET 4 Web Forms applications 6     Integrating ASP.NET MVC 3 into existing upgraded ASP.NET 4 Web Forms applications 7

This is the references you must added.

  • Microsoft.CSharp
    • (as this was a C# app)
  • System.Web.Mvc
    • From \Program Files (x86)\Microsoft ASP.NET\ASP.NET MVC 3\Assemblies
  • System.Web.WebPages and System.Web.Razor
    • From \Program Files (x86)\Microsoft ASP.NET\ASP.NET Web Pages\v1.0\Assemblies
  • System.ComponentModel.DataAnnotations

Next, add these sections to the Web.config. Again, it’s easier to use a diff tool and you might have a little trial and error.

Thought: This might be a nice NuGet package for someone to make…

Add these settings in appSettings:

1
2
3
4
<appSettings>
   <add key="ClientValidationEnabled" value="true"/>
   <add key="UnobtrusiveJavaScriptEnabled" value="true"/>
</appSettings>

Add these assembly elements under compilation:

1
2
3
4
5
6
7
8
9
<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" />
  </assemblies>
</compilation>

Add these namespaces in pages:

1
2
3
4
5
6
7
8
9
10
11
12
<system.web>
  <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>
</system.web>

If you’re running IIS7 at some point,  you will, add these:

1
2
3
4
<system.webServer>
  <validation validateIntegratedModeConfiguration="false"/>
  <modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>

And finally add this assembly binding redirect, just in case you’ve got ASP.NET MVC 1 or 2 assemblies in your Global Assembly Cache (GAC).

1
2
3
4
5
6
7
8
9
10
<configuration>
 <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

Also, make sure you merge in the Global.asax.cs , so that your Routes are registered at application startup.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public class SomeHybrid: System.Web.HttpApplication
{
    public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
        filters.Add(new HandleErrorAttribute());
    }
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        );
    }
    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        RegisterGlobalFilters(GlobalFilters.Filters);
        RegisterRoutes(RouteTable.Routes);
    }
}

Now, you can visit both pages. The WebForms page is a file on disk, so ASP.NET routing passes requests directly on to this page  /default.aspx. The ASP.NET Routing engine is engaged so you can also hit /Home/Index.

Leave a Reply

Your email address will not be published.