ASP.NET Webforms VS MVC

New Page 2

Comparison 

ASP.Net Web forms ASP.Net MVC
wrong
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.
right
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.
wrong
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.
right
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());
}
}
  
wrong
View and code behind tightly coupled so we can’t load different views for the same request
 
right
Completely decoupled. We can call different views within the same action and same view can be used for different requests.
  • Search project in OMNI
Ex 

public ActionResult Index(string deviceCompatibility)
{
if (deviceCompatibility == "Mobile")
{
return View("MobileView");
} else
{
return View("BrowserView");
}
}
wrong
Flexible combination of view and data is not possible right
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.
wrong
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());
}

right
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");
}
}

right
Less time to do development as ASP.Net is designed for RAD. wrong
Takes more time for development compared to traditional ASP.Net development.
right
We can have server side controls such as TextBox, DropDownList etc., which can be accessed in code behind. wrong
We cannot access UI elements in Controller actions. We can only bind model to view.
right
We have inbuilt ViewState to maintain the state of the controls between the postbacks. wrong
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.
right
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.
right
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.
wrong
We cannot create REST api with web forms project. right
MVC framework provides the option to create REST api.