Home
    Shop
    Advertise
    Write For Us
    Affiliate
    Newsletter
    Contact

Decide Between MVC and Web Forms for Next ASP.NET Project

ASP.NET MVC is new technology that becomes very popular. However, MVC is not replacement for Web Forms. Both will stay with us in longer period and be supported by Microsoft. The next logical question is: Which technology is better? Or at least which is better for my next project?

 

The answer depends of your organization and type of your project. Check these questions to find out which is better choice for your specific case:

Do you need TDD (Test Driven Development)?

ASP.NET MVC framework enables testing of each function separately. Because Model, View and Controller roles are clearly separated, this makes testing less complex. You can write your tests first and then develop application logic. On opposite, Web Forms application by default uses single file for all three roles which makes unit testing complicated. In MVC you can test Controller or Model separately, even if View is not existing yet.

Also, TDD in MVC is very customizable. You can use MS Test, NUnit, MBUnit or other testing framework you prefer. So, if your project requires Test Driven Development there is a strong argument for MVC.

Do you need complete control over generated HTML output?

Both ASP.NET Web Forms and MVC Views use .aspx files. MVC Views could have code behind like Web Forms. The main difference between Views and Web Forms is that View haven't <form > tag with runat="server" attribute. In code behind there is no page Init or Load methods, no event handlers. There is only class declaration inherited from ViewPage class (unlike Web Forms that inherit Page class). There is no form's controls with client IDs and no ViewState object. MVC HTML output contains only what you put in it with no surprise in form of large hidden field or JavaScript. Because of this, MVC pages are usually smaller than Web Forms, load faster and spend less bandwidth.

Do you need URL mapping?

If you choose ASP.NET MVC, all visitor interactions are automatically routed to Controller. For MVC, URL is not associated with some page, physically stored on server. This makes URL mapping easier which is good for search engine optimization (SEO). Web forms by default don't support this feature. In Web Forms application, every user request is associated with some .aspx page.

However, this is just default behavior of Web Forms, not their only behavior. It is possible to enable routing or URL rewriting with Web Forms too. There are numerous ways to do this: by using Application_BeginRequest event in Global.asax file, using routing in web.config, using custom HttpHandler (there are some complete free solutions, like UrlRewriter.Net), using IIS, ISAPI etc. More about URL rewriting you can find out on URL Rewriting in ASP.NET tutorial.

We can say that MVC has small advantage in this issue since MVC supports URL routing by default, but that is not significant advantage.

Do you need to support multiple views for your application (HTML, XML, mobile, REST API etc.)

Because of role's separation, multiple views can work with single model or controller. Then for different clients you can use different views. Let say you created feature rich View for visitors with Ajax support. Now, you want to support visitors with older browsers or with JavaScript disabled. Just create new View. You want to add RSS XML or support devices? Again, create one more View. All this Views could use same Model and same Controller. On this way, application is less complex and easier to maintain.

Do you need separation of concerns?

ASP.NET Model-View-Controller framework clearly separate different roles: data access code is in Model, output is handled in View and code for user interaction is in Controller. On this way large applications become less complex and easier for testing. Web Forms has only code behind file and in many cases, all code is in single file. But, that is just an easy option. Nobody stops you to organize code differently if you use Web Forms. There are number of suggestions (e.g. MVP - Model-View-Presenter) about how you can create n-tier web applications with Web Forms. However, since MVC separates roles by default, this is little advantage of MVC.

Can you work without Web Forms data controls?

If your application works with a lot of data which should be presented in grids, sorted, paged, edited in details view etc., Web Forms probably requires less work than MVC. ASP.NET Web Forms include data controls like GridView, DataList, ListView, Repeater, DetailsView and FormView. These controls offer rapid application development and you probably can finish your work much faster than if you use MVC. Also, there is a lot of commercial data controls from independent vendors with a lot of predefined styles and almost every feature you can imagine.

Conclusion

Decision between MVC and Web Forms is often subjective; if you are coming from Windows development you probably like Web Forms because of control's events. Your new project probably can be created with any MVC or Web Forms. Whatever you choose, it is good tool if you know it well. For example, URL mapping is naturally supported by MVC, although Web Forms work differently by default that is not big problem. You can simply copy/paste HttpModule for URL rewriting or use ASP.NET Routing in Web Forms project to get same results.

If you answered yes to most of these questions, than MVC is probably better choice. If after answering to questions above you still not sure which technology is better for your next project, use simply what you love more or what is more natural to your team development process. If needed, it is possible to mix Web Forms pages with MVC on same project.

Happy programming!


Tutorial toolbar:  Tell A Friend  |  Add to favorites  |  Feedback  |   Google