ASP.Net Web forms |
ASP.Net MVC |
|
ViewFirst -> Request will be sent to the View first and then
appropriate action (Event in the code behind) will be called.
Page life cycle will be executed before calling the action. |
|
Action first-> Request will be sent to Action (Method in the
controller) first; upon execution of code in the action it decides which
view to send in response.
No page life cycle in MVC. |
|
There will be no request served without view / HTML response.
If we want to get any other type of response for a particular request,
we need to handle it with Content-Type directive and Response.End which
is a tedious process. |
|
Response can be of any type (HTML /XML/JSON). View is not mandatory
for every request.
Ex
public ActionResult Index(string viewType)
{
if (viewType == "JSON")
{
return Json(new Customer(), JsonRequestBehavior.AllowGet);
} else
{
return View("EmployeeInfo", new Customer());
}
}
|
|
View and code behind tightly coupled so we can’t load different
views for the same request
|
|
Completely decoupled. We can call different views within the same
action and same view can be used for different requests.
Ex
public ActionResult Index(string deviceCompatibility)
{
if (deviceCompatibility == "Mobile")
{
return View("MobileView");
} else
{
return View("BrowserView");
}
}
|
|
Flexible combination of view and data is not possible |
|
Flexible data combination possible, which means we can pick up the
same model and bind with different views. Different models can be bound
to different views. |
|
We can do unit test for code behind as the code behind inherited for
“Page” class, which has a lot of dependencies. Below example will
throw an error.
Ex
[TestMethod]
public void TestMethod1()
{
WebApplication22.WebForm1 obj = new WebApplication22.WebForm1();
obj.Button1_Click(this, new EventArgs());
}
|
|
In case of MVC this becomes a normal class. A class which can be instantiated in simple unit test project and you can test various
aspects like session, viewbag, and tempdata in an easy way.
Ex
public class HomeController: Controller
{
public ActionResult Index()
{
Session["SomeSession"] = "Is this set";
return View("SomeView");
}
}
|
|
Less time to do development as ASP.Net is designed for RAD. |
|
Takes more time for development compared to traditional ASP.Net
development. |
|
We can have server side controls such as TextBox, DropDownList etc.,
which can be accessed in code behind. |
|
We cannot access UI elements in Controller actions. We can only bind
model to view. |
|
We have inbuilt ViewState to maintain the state of the controls
between the postbacks. |
|
We do not have a concept called ViewState in MVC as we don’t use
server side controls. However Razer / ASP view helpers provide the
mechanism to maintain state. |
|
No learning involved as we already have experience in ASP.Net
webforms. |
|
The learning curve is high. Developers from ASP.Net web forms
background have to change their thought process to analyze the request
flow. |
|
We can make use of Ajax toolkit for partial post backs or Ajax
request. |
|
We need to go for any Java script framework such as jQuery or
Prototype to implement partial post backs which takes development time.
Since MVC4 Razer engine also provides AjaxHelpers and AjaxExtensions to
implement partial postbacks. |
|
We cannot create REST api with web forms project. |
|
MVC framework provides the option to create REST api. |