ASP.NET MVC - Some Frequently Asked Questions
This article introduces ASP.NET MVC and answers some frequently asked questions about ASP.NET WebForms vs ASP.NET MVC.
What is MVC?
MVC or the Model-View-Controller is an architectural pattern used in software engineering for separating the components of a Web application. The MVC pattern helps decouple the business logic from the presentation layer which in turn gives you the flexibility to make changes to a layer, without affecting the other. This also leads to effective testing and maintainability.
The implementation of this pattern is divided into three parts:
- Model – Represents the domain specific data
- View – UI Components responsible to display the Model data
- Controller – Handles User Interactions/Events, manipulates and updates the model to reflect a change in the state of an application.
What is ASP.NET MVC?
ASP.NET MVC is a web development framework that embraces the MVC architecture. It is a part of the ASP.NET framework and provides an alternative way to develop ASP.NET Web applications.
Microsoft started working on the ASP.NET MVC framework in October 2007 and after a series of Previews and Beta Releases, ASP.NET MVC 1.0 was released on 17th March 2009. As of this writing, ASP.NET MVC 2 Preview 1 has been released
How is ASP.NET MVC different from ASP.NET WebForms (ASP.NET WebForm VS ASP.NET MVC)? Is ASP.NET MVC a replacement for WebForms?
No. ASP.NET MVC, is not a replacement for WebForms. Both ASP.NET MVC and ASP.NET WebForms are built on top of the Core ASP.NET Framework. In fact a lot of features we use in ASP.NET such as Roles, Membership, Authentication and a lot of namespaces, classes and interfaces can be used in an ASP.NET MVC application
Here are some points that differentiate ASP.NET WebForms from ASP.NET MVC:
ASP.NET WebForms
|
ASP.NET MVC
|
Uses the ‘Page Controller’ pattern. Each page has a code-behind class that acts as a controller and is responsible for rendering the layout.
|
Uses the ‘Front Controller’ pattern. There is a single central controller for all pages to process web application requests and facilitates a rich routing architecture
|
Uses an architecture that combines the Controller (code behind) and the View (.aspx). Thus the Controller has a dependency on the View. Due to this, testing and maintainability becomes an issue.
|
ASP.NET MVC enforces a "separation of concerns". The Model does not know anything about the View. The View does not know there’s a Controller. This makes MVC applications easier to test and maintain.
|
The View is called before the Controller.
|
Controller renders View based on actions as a result of the User Interactions on the UI.
|
At its core, you ‘cannot’ test your controller without instantiating a View. There are ways to get around it using tools.
|
At its core, ASP.NET MVC was designed to make test-driven development easier. You ‘can’ test your Controller without instantiating a View and carry out unit-tests without having to run the controllers in an ASP.NET process.
|
WebForms manage state by using view state and server-based controls.
|
ASP.NET MVC does not maintain state information by using view state.
|
WebForms supports an event-driven programming style that is like Windows applications and is abstracted from the user. The State management is made transparent by using sessions, viewstate etc. In the process, the HTML output is not clean making it difficult to manage later. The ViewState also increases your page size.
|
In ASP.NET MVC, the output is clean and you have full control over the rendered HTML. The orientation is towards building standard compliant pages and provides full control over the behavior of an application.
|
Deep understanding of HTML, CSS and JavaScript is not required to a large extent since the WebForm model abstracts a lot of these details and provides automatic plumbing. While abstracting details to provide ease of use, sometimes a solution is overcomplicated, than it needs to be.
|
A thorough understanding of how HTML, CSS and JavaScript work together is required. The advantage is you can do a lot of jQuery and AJAX stuff in an efficient and simple manner than you would do in an ASP.NET application.
|
WebForms can drastically reduce time while building up intranet and internet applications that use a lot of controls (drag and drop model). Although this is true for development, a lot of time is spent later to code around limitations.
|
You lose the 'drag and drop' quick model of building your web applications. The focus is on control over the application behavior and test-driven development. The model is extensible and you do not have to spend time working around limitations.
|
Relatively simple to learn and pickup. Works very well for developers who initially have trouble with the HTTP/HTML model and are coming from a similar WinForms oriented event model.
|
There is a learning curve to understand the why, when and how of ASP.NET MVC.
|
Lesser amount of code is required to build webapps since a lot of components are integrated and provided out of the box. You can also use a lot of data controls provided out of the box that rely on ViewState.
|
Since the application tasks are separated into different components, amount of code required is more. Since ASP.NET MVC does not use ViewState, you cannot use Data controls like GridView, Repeater.
|
Works very well for small teams where focus is on rapid application development
|
Works well for large projects where focus in on testability and maintainability.
|
Remember there is always a trade-off in adopting a certain technology. We also cannot conclude that one model is better than the other. You need to decide upon various factors and do a thorough feasibility study before you go in for a certain methodology of developing your applications. Use the tool best suited for the job. You do not have to do something just because others are doing it!
I would like to quote Rex Morgan as he put this point brilliantly in the stackoverflow forums.
“It's important to keep in mind that MVC and WebForms are not competing, and one is not better than the other. They are simply different tools. Most people seem to approach MVC vs WebForms as "one must be a better hammer than the other". That is wrong. One is a hammer, the other is a screwdriver. Both are used in the process of putting things together, but have different strengths and weaknesses.
If one left you with a bad taste, you were probably trying to use a screwdriver to pound a nail. Certain problems are cumbersome with WebForms that become elegant and simple with MVC, and vice-versa”
I find ASP.NET WebForms easier. When and Why should I Care about ASP.NET MVC?
Again, ASP.NET MVC is not introduced to replace WebForms. WebForms has been amazing in its own arena, but depending on your experiences with it and how far have you have exploited it’s usage in your applications, there are difference of opinions as far as its advantages and disadvantages are concerned.
Here are some points that will help you understand and embrace ASP.NET MVC.
ASP.NET MVC is for you if you –
- care to build applications that are maintainable, testable and are ‘abreast’ with the other development methodologies, a couple of years from now.
- emphasize on reducing complexity by enforcing ‘separation of concerns’ and introducing loose coupling. Tasks are separated and handled by separate components.
- are tired of dealing with postback and viewstate issues
- choose testability (Test Driven Development) over rapid application development(rad) where in rad, a single class is responsible for displaying output and responding to events. This behavior couples the layers tightly, making it difficult to test.
- have a large team of developers and want to promote parallel development where there are separate teams working on the view, controller and model.
- want to provide your application with multiple user interface. Since there is no or little dependency between different components, you can adopt a pluggable UI model in your application keeping the same business components.
- are worried that your smart-client look-a-like, tightly coupled and stateful abstracted webform model is difficult to test and breaks frequently while maintaining application.
- want a simple, seamless and maintainable AJAX experience like other platforms provide
- want meaningful, RESTful URL’s
- need to work on multiple platforms later. A shift from ASP.NET MVC to Ruby on Rails and other similar platforms is easier. It’s also a good option to consider for your career.
My advice to all you developers out there would be to go ahead and pick up this new technology, develop a sample MVC project and you will soon realize that you are following architectural best practices, are closer to the way web works and are using AJAX and JQuery efficiently to deliver jaw dropping UI experiences. A lot of our development time has gone in measuring the height and width in your apps – it’s now time to take a dive into the depth of it and see how things are done the right way – the web oriented way. After all, we will be trying a framework based on a pattern that has been used for over 30 years now! So the ‘it will work’ tests have been done for us.
I am still doubtful. How will companies adopt ASP.NET MVC ?
Now that’s a tricky question. There is an investment required here in terms of resources and adopting a new technology. With the economy on a downtrend, the adaptability will be slower initially than Microsoft would have expected. Since both the frameworks, ASP.NET WebForms and ASP.NET MVC, are a product of the same company, it will be the client’s need and the project’s interest that will drive the show. As I said, it’s a tricky question!
Where can I download ASP.NET MVC from? Is it Free?
Yes, ASP.NET MVC is absolutely free. You can download ASP.NET MVC 1.0 from here
Does Microsoft have a Roadmap for ASP.NET MVC? Is Microsoft keen on taking this ahead?
There is an ASP.NET MVC Roadmap. The ASP.NET MVC framework is well maintained well documented and there are plenty of tutorials, videos and hands on labs available. There is a clear roadmap too. I feel this framework is here to stay and it will be fun watching how this architecture evolves with ASP.NET over the period of time.
What about Migration from one to the other? Can we also create an Application using ASP.NET WebForms and ASP.NET MVC?
In some cases, Yes. I would say that both the technologies complement each other, but migrating from WebForms to ASP.NET MVC will not be a piece of cake depending on the size of the projects. People, who are keen to use WebForms only and focus only on separation of the logic and UI, can adopt the MVP Pattern. However the MVP pattern is not as effective as the MVC.
If you are planning on developing an ASP.NET WebForms application and plan to move it to ASP.NET MVC in future, then try to build a loosely coupled architecture. This way the pains of migrating will be less. Moreover since ASP.NET MVC focuses on plugability and can be extended, functionality like Routing can be used in ASP.NET WebForms and the same can be utilized in ASP.NET MVC later.
Honestly, although I have not yet built an application that involves both ASP.NET WebForms and ASP.NET MVC, it does look possible to build one, since both the technologies are built on top of the core ASP.NET framework.
What is the best way to learn ASP.NET MVC?
Start using it! There are some additional resources, learning tutorials and articles listed over here that makes the learning curve smoother –
- Learn ASP.NET MVC – ASP.NET MVC Written Tutorials, Video Tutorials, Sample Applications
- Rob Conery’s MVC StoreFront Series – In Rob’s own words, the goal of the series is to explore ASP.NET MVC and the various disciplines that compliment it. Rob has covered all kinds of developer goodness, including Test-driven Development, Dependency Injection, and (lightly) Domain Driven Design in these series.
- Professional ASP.NET MVC 1.0 – A great book by Rob Conery, Scott Hanselman, Phil Haack and Scott Gutherie begins with a complete ASP.NET MVC reference application and then takes you through the basic and advanced features of ASP.NET MVC.
Further Reading:
In the next article, we will see how to build a sample ASP.NET MVC application.
Suprotim Agarwal is an ASP.NET Architecture MVP and the founder of this website - www.dotnetcurry.com. You can read more about Suprotim over here
This article has been editorially reviewed by Suprotim Agarwal.
C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.
We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).
Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.
Click here to Explore the Table of Contents or Download Sample Chapters!
Was this article worth reading? Share it with fellow developers too. Thanks!
Suprotim Agarwal, MCSD, MCAD, MCDBA, MCSE, is the founder of
DotNetCurry,
DNC Magazine for Developers,
SQLServerCurry and
DevCurry. He has also authored a couple of books
51 Recipes using jQuery with ASP.NET Controls and
The Absolutely Awesome jQuery CookBook.
Suprotim has received the prestigious Microsoft MVP award for Sixteen consecutive years. In a professional capacity, he is the CEO of A2Z Knowledge Visuals Pvt Ltd, a digital group that offers Digital Marketing and Branding services to businesses, both in a start-up and enterprise environment.
Get in touch with him on Twitter @suprotimagarwal or at LinkedIn