CodeProject (original) (raw)

Is ASP.NET MVC replacement for Web Forms? No, Both have there pros and cons. Lets take a look at same.

Introduction

It’s been a long time I have been working as an ASP.NET developer and I do enjoy writing web apps using web forms.

In 2008 Microsoft came up with something called as ASP.NET MVC and I was quite amazed about the fact “Why one more ASP.NET technology required” and many people still pondering same.

Many people say ASP.NET MVC replaced ASP.NET Web Forms. But that’s not true. Both have their own pros and cons Nobody can tell or teach us what to use and when, but we can discuss about some facts which will help us to make choice between both of them. And this article contains same.
We also try to find answers for some questions like, What is ASP.NET? What is ASP.NET Web Forms? What is MVC? What is ASP.NET MVC?

If you are advanced or experienced ASP.NET MVC developer, who already know MVC very well then this article will help you to revise your concepts.

Index/Contents

Talk about Visual

Microsoft initially came up with something called Visual. With the help of technologies like Visual Basic, Visual C++ Microsoft enabled RAD (rapid application development) of Graphical User applications. Features such as Drag and Drop and intellisense made developers to focus more on the business functionality of the application rather than on UI design.But this technologies were limited to desktops, when it comes to web the only option left with Microsoft was ASP.

Web Technologies

When we say web technologies, we have classic ASP, php, jsp,ROR, ASP.NET Web Forms, ASP.NET MVC and many more. Classic ASP is one of the web technology introduced by Microsoft. Biggest pain point with the classic ASP was spaghetti code and maintainability. Let assume a scenario where you have some text boxes and a button. On button click you validate the data with the server and if validation succeeds data will be stored into database and in case it fails, error message will be shown to user in the form of red colored label. You know what’s the biggest problem with this scenario is? You have to do lots of stuffs by your own.

  1. First make the self-post back by setting form’s action attribute’s value to same page.
  2. Text box values are going to be cleared on button click, so only choice left will be retrieving values from posted data.
  3. In case validation fails you have to explicitly
    1. Set all values back to the corresponding text boxes from posted data
    2. Show error message.

(Using Ajax is an alternate method. Here I was trying to explain the manual work need to be done with classic ASP)

Visual in Web

Technologies like Visual Basic are limited to standalone applications, when it comes to web, only option left with Microsoft is classic ASP.

When we talk about Web and Desktop, two things which should be considered are

  1. How state management works?
  2. How request/response works?

Web works on HTTP which is completely a stateless protocol. We will be having request and response independent of previous request and response. Unlike desktop no variable values will be preserved and no complete event driven programming model. Like desktop it will wait for user inputs but every user input and interaction act as a new request (get/post) to server.

Finally Microsoft came up with something called ASP.NET Web Forms, considering rapid application development and easy learning in priority.

What is ASP.NET?

ASP.NET is a Microsoft’s Web application framework built on Common language runtime for building dynamic web sites using one of the programming languages like C#, VB.NET etc. It supports 2 models Web Forms and ASP.NET MVC.

What is Web Forms?

Microsoft first brought out ASP.NET Web Forms from ASP which solved lots of problems by creating higher level abstraction over stateless web and simulated stateful model for Web developers. In web forms concepts like self postback (post form data to same page) and ViewState (maintain control values during postbacks) are introduced. And the most interesting part is it’s not required to write even a single line of code. With Web Forms Microsoft tried to bring the Visual Basic model into web.

Let’s talk about advantages and disadvantages of Web Forms.

Advantages:

Disadvantages:

ASP.NET 4.0

ASP.NET 4.0 has come up with some good features to overcome some of the above problems

Even after the evolution of the revolutionary features of ASP.NET,

  1. It was still not able to solve the problems of unit testing
  2. I never saw any ASP.NET Web Form developer who tries to disable the ViewState.Smile | <img src= (It’s my perspective, no hard feelings please.)
  3. We got some control over ID of elements, but not complete control over HTML, still have problem to implement JavaScript.

What is MVC?

MVC is an architectural pattern which is has been around for sometimes now.
Many are using it including Java. It’s not new concept which Microsoft brought it up. ASP.NET MVC is something we should talk about. But prior to that lets clear some terminologies including MVC.

Model is considered as smart and handles the Business rules, logic and data and will be independent of other parts of MVC (controller and View). Controller receives and dispatches the request in short it handles the user interaction and input logic. It knows about both Model and View. A view is considered as dumb and is an output representation of model data. It may be an excel sheet, a web page showing list of records or just a simple text. View knows about only Model.

What is ASP.NET MVC?

ASP.NET MVC is a Microsoft’s one more Web application framework designed with separation of concerns and testability in mind. It is built on CLR and completely based on MVC architecture and so we think in terms of controllers and Views. ASP.NET doesn’t have support for ViewState and server controls, so we get feel of old web here. Let’s talk about Advantages and disadvantages of ASP.NET MVC

Advantages:

Also user-friendly and readable URL improves SEO.

Disadvantages:

How ASP.NET MVC works

  1. User makes the request for some resource in server (by putting some URL in the browser).
  2. Request comes to controller first (routing engine is the one who is responsible for deciding which request will be handled by which controller. In this article we won’t talk in depth about this behavior).
  3. Controller if required talk to model for data.
  4. Model operates on database (or on some other data sources) and return data (in form of business objects) to controller.
  5. Controller chooses the appropriate view (like say Customer view which will may contain some html tables, drop downs, textboxes…).
  6. Controller passes the data (model data retrieved in step 4) to chosen view(in step 5), where data will be populated as per convenience.
  7. Controller sends back view to the user.

This was for get request, same happens for post. Only instead of putting URL in the browser user will do some action on already requested page and flow start with the controller. Actions like clicking button, changing drop down value etc.

Why ASP.NET Web Forms and Why ASP.NET MVC?

Each can be the “best choice” for a particular solution depending on the requirements of the application and the background of the team members involved. What to choose and when has more to do with business prospective than which one is better than other. When facing a decision to choose between ASP.NET Web Forms or ASP.NET MVC it is important to know that neither technology is meant to replace the other.

Two important factors you should consider while making the choice is

  1. Rapid application development - If you want to develop anything rapidly ASP.NET Web Forms is the only chance you are having, you can’t even consider for ASP.NET MVC for RAD. (Reasons for RAD may be anything like client is not paying too much, or application is going to be used for only one or two months and won’t require much maintenance.)
  2. Unit Testing - If automatic unit testing is most important factor for you MVC will be best for you.

Other than these, what you can do is, write down all your project requirement and try to compare them with Pros and Cons of both Web Forms and MVC and if possible try to ask yourself following questions and point MVC and Web Forms accordingly

  1. Does your team have good experience with Web Forms or Windows Forms?
    Well, if yes then probably learning ASP.NET MVC is going to be a tedious task for team, because developers have been used to with ViewState and event driven programming by now and migration is going to be a difficult task.1 point to Web Forms.
  2. Does your team have good experience with ASP.NET MVC?
    If yes ASP.NET MVC get 1 point
  3. Does your team have experience on ASP or non-Microsoft technologies such as android, ios, JSP, ROR, PHP?
    If you have been JSP or ASP developer prior then you might be familiar with HTTP get and Post and even u might have hands on with MVC because most of them use MVC by default. It gives 1 point to ASP.NET MVC.
  4. Is JavaScript going to be used extensively?
    If Yes, MVC gets the point because you get complete control over HTML. 1 point ASP.NET MVC.
  5. Looking for good performance?
    With no support for ViewState ASP.NET MVC provides good performance gain over traditional ASP.NET Web Forms.1 point ASP.NET MVC.
  6. Planning to reuse the same input logic?
    If yes stick with MVC.

Conclusion

I think you should have equipped with enough information to make a decision what is best for your project. The complete decision depends on your team and project requirement.

Hope all of you enjoyed reading this article. Thank you for the patience.

For technical training related to various topics including ASP.NET, Design Patterns, WCF and MVC contactSukeshMarla@Gmail.com or at www.sukesh-marla.com

For more stuff like this click here. Subscribe to article updates or follow at twitter @SukeshMarla

Also go through our complete step by step series of MVC(Model View Controller)

Click here for see MVC video step by step.