Reputation: 4634
I have on request from a client built a huge site with ASP.NET Web forms. The problem is that I'm finding ASP.NET Web forms to be somewhat unintuitive (my personal taste only). So what I would like to do is use MVC but I can't really expect my client to pay for my time on a complete rewrite (nor do he have the time).
So what I'm asking is, can I use ASP.NET MVC and Web forms at the same time and gradually use MVC more and more? Any tips?
Upvotes: 31
Views: 9038
Reputation: 8289
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.
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.
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)
Upvotes: 17