Wednesday 8 May 2013

To use ASP.NET WebForms or Model-View-Controller (MVC)?

ASP.NET is one of the most popular web development technology that was introduced by Micro$oft. The “native” web dev in ASP.NET is done using WebForms. WebForms are essentially HTML files (but with file extension .aspx) with both client side and server side scripting that is rendered as a web page on runtime. The server-side code can either be placed inside the ASPX files themselves or as “code-behind” .cs files.
aspnet
The WebForms have been in use since several years now and is standardized by Micro$oft as its primary web dev platform. It has drag and drop functionality as the IDE provides the well-known toolbox. The drag and drop function works in the same way as in desktop .NET applications. Much of the initial code is generated automatically and the intellisense feature works perfectly on the server-side code mixed with HTML syntax.
What-Is-MVC
But then a new way of coding ASP.NET websites emerged that looks very promising. It is called Model-View-Controller (MVC). Breaking away from the usual mixed up approach of WebForms (and even other webdev platforms like PHP), MVC tries the “division of labour” approach. In MVC the entire coding is logically divided into three kinds:
  1. Model means a template and basic structure over which the entire software is built. It is responsible for setting up database connections, file associations and remote resources that the application itself will be using/consuming during design and/or runtime. Using MVC we keep this part separate from others and usually this one is the first to be attended.
  2. View is the interface of the application. It is responsible for displaying information to the user and also taking input from the user. The interface should be easy to use and pleasing to see. MVC segregates this part from others so that the designer can work from the model up without worrying about any changes to the basic structure or any other additions.
  3. Controller (as the name suggests) controls the entire application and co-ordinates the Model with the View to provide a streamlined execution of the application. Controller in MVC is built according to the user interface and it often interacts directly with the components.
In MVC there are no .aspx type pages with code embedded or code behind. Instead it has “Controllers” that have methods for every page. Instead of invoking a webpage name like http://example.com/page1.aspx the name of the method itself is used with any GET parameters like this-
Here, page1 is actually name of a method and var1 and var2 are arguments to the method! The controller method will be something like this-
1: public ActionResult page1(string var1, string var2) {
2:   ViewBag.Message = “Var1 = ” + var1 + “ and var2 = “ + var2;
3:   return View();
4: }
Another question comes in the head- Is the page design also kept within that method itself? No. For this functionality “View” is used. The design for the controllers is made inside these views. They can have design with code embedded (no- there’s no “code-behind” stuff used in MVC sadly). For server-side code either of these two methods can be used- ASPX or Razor. However, they are beyond the scope of this article.
"So, which one should I use to make my website?" The answer is not very difficult. When there’s a big project with at least more than 3-4 people then MVC should be the ideal choice because it gives much better co-ordination in a team situation. However, when there is a small to medium level project that involves at most 1-3 people then WebForms should be fairly good for that.
Also, to mention database connectivity and passing data to the interface is a bit easier in WebForms than MVC. Many people get stuck while operating database in an MVC application because the contact of the model (database connector and data retriever often) and the view (interface) is rather sophisticated. Usage of LINQ however makes it a bit straightforward but surely most developers accustomed of using SQL commands and stored procedures will not like it at first site.

4 comments:

  1. Hi there are few spelling mistakes but I really like the article :) thanks for sharing with us

    ReplyDelete
    Replies
    1. Thanks for your reply! I will try and remove the errors :>

      Delete
  2. i hv a cat who lieks this blog :P

    ReplyDelete
    Replies
    1. Oh :D I am glad your cat likes it :> thanks to you and her!

      Delete

All Articles © Asit. No Copying. Powered by Blogger.
 
Copyright © . The Tech Veda - Posts · Comments
Markup Powered by Bluefish Editor 2.2