Can I use ASP.NET MVC together with regular ASP.NET Web forms
Update 2014:
Visual Studio 2013 brings us closer to One ASP.NET. There’s no MVC project type or Web Forms project any longer, there’s just ASP.NET. If you want to mix Web Forms and Web API, or MVC and SignalR, go ahead! You are encouraged and supported. New features and functionality are brought in with NuGet without breaking existing apps.
VS 13
So the mixing of Web Forms and MVC (which has been pretty much been working seamlessly) is now encouraged and the boundaries are obfuscated. I guess MS recognized the need to allow projects to slowly migrate to MVC, or just migrate portions as needed, and gain the best of both worlds.
MVC Project:
Having used MVC with Web forms for quite some time it is certainly possible and an excellent choice. New functionality and pages can be easily added into MVC pages with good architectural design such as DDD (Service Repository, Dependency Injection etc.) and the old stuff can stay as it is. Combining MVC inside a webforms page also works fine, although there can be some minor issues with JS validation. I would highly recommend it.
Its fairly easy to start with creating an MVC application (5 atm) and then after you get the basic template up and running add the old webforms inside a folder. This way you get the new MVC setup correctly and it retains backwards compatibility.
- You can create a MVC project and just copy all the Webforms pages into e.g. a folder. (possible to just upgrade a project too, I'd imagine)
- Setup routing to ignore requests with that particular foldername in the URL. This way MVC and webforms can be used together without hassle in the URLs.
- Asp.net WebForms aspx pages can reside within MVC as long as they're not placed inside the /Views folder.
- Go with Razor over the old syntax, you can customize HTML helpers easily and it's a lot cleaner.
- Check into MVC standards and conventions, the controller should be very lightweight. It's not the code-behind which can contain as much logic as you need.
- Views shouldn't contain more than presentation logic (no business logic)
- Everything new => MVC
- Resources, web.config etc. are usable by both
- Menus don't use the old sitemaps in MVC
Using MVC inside a webforms .aspx page:
The text below is meant to demonstrate how you can utilize MVC from inside a webforms page. (For example inside MyPage.aspx) You can use MVC actions/views inside webforms with e.g. ajax to fill a portion of the page or certain div's.
Webforms containing MVC works fine, at least when adding an ajax call inside the HTML to fill a div from MVC.
Inside an .aspx
..html & webforms code
<div id="fillMeFromMVC">
<script type="text/javascript">
$.ajax(... call an MVC action to fill
the "fillMeFromMVC" div that this script sits inside of);
</script>
</div>
This will fill a portion of the page via MVC and you can cleanly do your MVC without worrying about what's done in webforms.
WebForms vs. MVC:
By this point you are likely pretty aware of the differences between the two technologies, however here is a little comparison between them. They both have their purposes and uses. I personally prefer MVC for the things I have needed to do, however it likely depends on what you are trying to achieve.
If you use e.g. a SOA behind them both the webforms and MVC pages can utilize the same business logic. Webforms can be detrimental when the code is all in the page behind and is tied to the UI (no separation of concerns). With a solid architecture and a bit of effort that can be reduced though.
Further reading:
Webforms vs. MVC (Code project)
Difference betweeen ASP.NET WebForms and ASP.NET MVC (a blog)