TOC

This article is currently in the process of being translated into Arabic (~25% done).

Introduction:

ASP.NET MVC vs. Web Forms

لقد تم اطلاق اول اصدار من "ASP.NET" (تنطق "اي اس بي دوت نيت") في عام 2002, حيث كان محرك نماذج الويب هو الخيار الوحيد المتوفر. في وقت لاحق, لدعم "MVC" (تنطق "ام في سي") , قامت شركة مايكروسفت بتوسيع نطاق "ASP.NET" (تنطق "اي اس بي دوت نيت") لتدعيم محركات العرض المتعددة, لكن هذه لسنوات عديدة فقط, ملاحظة: اذا كنت تستخدم "ASP.NET" (تنطق "اي اس بي دوت نيت") , اذا انت تستخدم نماذج الويب تلقائيا.

كان لميكروسوفت هدفا جديرا بالثناء عندما قاموا بأنشاء الويب فورم او كما تسمي نماذج الويب لقد أرادوا استخلاص اكبر قدر من التفاصيل الجريئة لبرتوكول HTTP والطبيعة الغير محدودة له والتي تجعل تطوير الويب اشبه بتطوير تطبيقات الويندوز والذى كان تجربة ممتعة في ذلك الوقت.

لقد فعلوا ذلك من خلال تقديم حالة النموذج (ViewState)، والذي من شأنه التأكد من الحفاظ على الحالة الحالية لأي نموذج خلال عمليات إعادة النشر للخادم وفعلوا ذلك باستخدام عناصر تحكم الخادم، والتي تضمنت تحويل HTML وCSS إلى تحكم تعسفي التي يمكنك تخصيصها باستخدام الخصائص المنطقية بدلاً من إجبارها على مزج HTML وCSS مباشرة.

كما قاموا بتقديم النموذج المبني على الأحداث، والمعروف بالفعل لمطوري الويندوز في ذلك الوقت، لتمكين المطور من الاستجابة للأحداث الفعلية، مثل النقر على زر أو تغيير خانة الاختيار، بدلاً من القيام بالتحقق اليدوي لهذا في كل مرة يتم تحميل الصفحة هذا يعني أيضًا أنه تم فصل الترميز والرمز، وهو أمر رائع من الناحية النظرية

Where Web Forms Failed

Web Forms was a fresh breath of air for many developers, and it likely also helped a lot of new developers, or developers only familiar with Windows application development, to learn building applications for the web. Unfortunately Microsoft didn't succeed in creating the perfect and flawless abstraction, because a number of problems quickly emerged. Some of them were fixed in later releases, while others were more fundamental to the way WebForms worked and therefore harder to change. The Web Forms technology has been criticized mainly for the following things:

ViewState makes pages heavier

By keeping track of every server control on the page in a ViewState string, which is sent back and forth between the server on each request, Web Forms pages got quite a bit heavier. If you were building a medium complex page, the resulting ViewState string could lead to an increase of several hundreds of kilobytes. This could lead to longer load times, especially on a mobile connection and with the increase of traffic from smartphones all over the world, this became a very real problem.

Server controls limits your control over HTML output

Server controls makes it easy for you to quickly create something useful, but you never get full control of the HTML which it renders. This can become a problem when you need to fine-tune your work as well as if you experience browser compatibility problems.

Web Forms is bad for automated testing

The Web Forms model was introduced before automated/unit testing became a big thing and when it did, it was easy to see that Web Forms was hard, if not impossible, to effectively unit test.

Where ASP.NET MVC is an improvement over Web Forms

ASP.NET MVC removes a lot of the abstractions implemented by Web Forms. For instance, you usually generate all of the HTML yourself, instead of relying on server controls. There is also no longer any ViewState maintained for you, effectively eliminating that problem (but also rendering several of the server controls, like the GridView and the Repeater, useless at the same time).

The MVC model is perfect for automated/unit testing, because of the loose coupling between the different layers.

Which technology should you choose?

It's important to state that while Web Forms may seem like an outdated technology when reading the above, it's actually not at all - Web Forms is still being actively developed by Microsoft and is still a possible choice when entering the world of web development with ASP.NET. Web Forms is especially well suited for situations where you want something up and running in a hurry - the big amount of advanced server controls makes it easy to accomplish something very useful in a rush, at the price of the flexibility it gives you to write all the markup manually.

If you already know how to use Web Forms, you should definitely give ASP.NET MVC a try, especially if some of the above mentioned problems have been bugging you as well. If you're new to web development, and you need to decide between the two technologies, I would still recommend giving ASP.NET MVC a try. The MVC model can seem a bit restrictive to some people, and having to follow a pattern is obviously harder in the beginning than not following one, but once you get used to it, it's very pleasant to work with and judging from the amount of attention that the MVC model receives in general, it's not likely to disappear anytime soon.

Summary

So, while ASP.NET Web Forms might be a bit easier to get started with, you should probably give ASP.NET MVC a try first, if you're new to the world of web development. Don't worry, this tutorial will get you started and guide you through the process of developing your first ASP.NET MVC application.


This article has been fully translated into the following languages: Is your preferred language not on the list? Click here to help us translate this article into your language!