Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running ASP.NET Webforms and ASP.NET MVC side by side

Currently I'm working on Web project that is built using ASP.NET Web Forms. We want to start building new pages using MVC framework.

Looks like running MVC and Web Forms side by side is doable Running ASP.NET Webforms and ASP.NET MVC side by side

I'm curious if there are any problems or gotchas that I need to plan for.

I'm running on ASP.NET 4.0 and planning to use MVC3.

like image 581
dev.e.loper Avatar asked Jun 20 '11 16:06

dev.e.loper


2 Answers

I have been running Webforms and MVC together for an internal application here. It started off as a webforms application and I have migrated it into MVC2 (then 3) with several pieces still successfully working with Webforms still.

As Darin said, the main gotcha is going to be with templating. If you use the Webforms View Engine you have to create 2 layers of master pages. Webforms code (such as the script manager) do not run on MVC pages, and MVC code doesn't work under Webforms pages.

My master pages are setup with a global master page that doesn't contain any MVC or Webforms code. It only has CSS, global javascript, and main layout. I then have a MVC master page and a webforms master page, both have directives to use the global master page as their master pages. Then each webforms page uses the Webforms sub-master page and the MVC uses the MVC sub-master page.

If you need to put some code in the global master page, you can detect if the sub-page is a Webforms or MVC page by testing if Page is System.Web.Mvc.ViewPage. If that's true then it's an MVC page, otherwise it is a Webforms page.

However, if you decide to go with the Razor view engine (which I do recommend for MVC, it's so much better) it becomes harder. You have to do some additional work arounds past what I previously mentioned. This blog post should help in that regards.

like image 131
KallDrexx Avatar answered Nov 13 '22 05:11

KallDrexx


In addition to what Darin said, try to not use the WebForms view engine if you can help it and create proper URL routes so that pages that should be handled by WebForms are still handled by WebForms.

like image 21
Roman Avatar answered Nov 13 '22 05:11

Roman