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?
Luckily, the answer is yes. Combining ASP.NET Webforms and ASP.NET MVC in one application is possible—in fact, it is quite easy. The reason for this is that the ASP.NET MVC framework has been built on top of ASP.NET. There's actually only one crucial difference: ASP.NET lives in System.
Yes. MVC is just a different implementation of the IHttpHandler interface so both classic ASP.NET and ASP.NET MVC pages can coexist in the same app.
Asp.Net Web Form has built-in data controls and best for rapid development with powerful data access. Asp.Net MVC is lightweight, provide full control over markup and support many features that allow fast & agile development. Hence it is best for developing an interactive web application with the latest web standards.
More Control-The ASP.NET MVC framework provides more control over the HTML , JavaScript and CSS than the traditional Web Forms. Testability-ASP.NET MVC framework provides better testability of the Web Application and good support for the test driven development too.
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With