WebForm vs MVC

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:

(1)Web Forms supports Rich server controls.  

    • While working with pure HTML you might have noticed, Things are not always same at all place.
      A UI which looks great in IE might distract in Firefox or vice versa.
      ASP.NET server control detects the browser and generates appropriate html and if required JavaScript also.
    • Many server controls like GridView and ListView comes up with data binding capabilities reducing lots of efforts and codes being written.

(2)Support for ViewState

    You might have heard couple of times “HTTP is a stateless protocol”. Normally controls will not retain their values between requests. But in Web Forms statefulness is achieved by storing last known state of every control within the client page itself in the form of hidden field called ViewState.

(3)Event driven programming 

  With the help of

    •   Code behind
    •   Self postback mechanism (posting back form to the same page)
    •   ViewState

  Microsoft introduced event driven programming in internet world.

    Developer will no more rely on POST, GET methods for handling user interactions with server. For example she/he will drag control (say button) to page, just double click it to generate the code block for handling user’s click on server, write down the logic inside it. That’s it. She/he is not concerned with what happens inside.
(4)Rapid application development

  I don’t think any explanation is required for this. Rich server controls, Event driven model and ViewState increases the development speed by great extent, Developer will be abstracted from lots of the background complexities.

(5)Less learning effort

  Using strong server controls and ViewState developer can develop real world applications with minimal HTML and JavaScript skills.

Disadvantages:

(1)Project Architecture

    There is no fixed predefined Project Architecture for creating web applications when it comes to Web Forms. Developers have full flexibility for choosing their own architecture. 
    One may use basic three layered architecture dividing the system into UI, Business layer and Data access layer or a more advanced one like Model-View-Presenter. Even one may choose only code behind and write everything there which is not at all considered as good practice. Code behind is tightly connected to UI, ending up with some presentation logic.

(2)Unit Testing

    In Web Forms code behind ends up with lots of event handlers, making automatic unit testing an impossible task.

   Note: as per my knowledge even with the help of mock testing (using MOQ or rhinomoq) we can’t mock ‘sender’ and ‘eventargs’ in event handlers.

    And when we talk about employing TDD, unit testing code behind (presentation logic) becomes very important.

(3)Performance

   ViewState becomes solution for some problems with classic ASP but it also becomes an issue. ViewState is stored in the page itself resulting increased page size so reduced performance.

(4)Reusability

  • Let’s talk about another example where we are supposed to build 2 UI

    • Taxable Employee screen
    • Nontaxable employee screen

    Now most of the code behind logic is going to be same for both screens. 
    One solution will be, add some if conditions in code behind and create a single UI.

    • This will violate Single Responsibility principle which says there should be only one reason a software entity to be changed, because in this case employee form will be changed whenever any of the taxable/nontaxable information changes.
    • Secondly it may possible that both UI
    • Less Control over HTML

      In Web Forms many times we are not sure about what html we will get at the end making integration with JavaScript frameworks like jQuery a difficult task

    • SEO

      URL’s are pointing to fixed ASPX pages which might be decorated with some query string. They are not user friendly and affect SEO.

(5)Less support for parallel development - ASPX page are tightly coupled with code behind files. So it’s not possible that 2 different developers are working on one section (one on aspx and one on code behind) at same time.

ASP.NET 4.0

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

  • ViewState: Provide the way to disable or control the size of the ViewState. (But there is no compulsion or fixed rule that will say to do so.)
  • URL routing: Using URL routing we can provide our own URL in lieu of the physical path of the page.
  • ID: In ASP.NET 4.0 we have better control over Id of elements and thus integration with JavaScript framework become easier. (We still don’t have complete control over HTML being generated.)

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.  (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.

  • Patterns - In simple words Pattern is a solution to a problem in a context.
  • Architectural Patterns - Architectural Pattern is something which solves our problem at sub system level or in short module level. It deals with the problem related to architecture of a project. It tells us how we can divide our systems and especially why. We make Class libraries, Components, Web services to solve the problem.
  • MVC When we talk about application we will be having input logic, business logic and UI logic and MVC is an architectural pattern which let us develop an application having loosely coupling between each of these elements.
    The main intention behind MVC pattern is separation of concerns. It makes presentation or UI ignorant of business and user interaction logic.
    According to MVC system should be divided as M (Model), V (View) and C (Controller).
  • 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:

      • Project architecture -

        One of the advantages of using ASP.NET MVC is it enforces separation of concerns. So there is very less chances of getting things more complex.

Test Driven development and Reusability

    • In MVC controller is a separate class so automatic testing is possible featuring Test Driven Development.
    • Controllers are not bound to any specific view and so can be reused for multiple views

    .

      • Performance - ASP.NET MVC don’t have support for view state, so there will not be any automatic state management which reduces the page size and so gain the performance.
      • Full control over HTML - ASP.NET MVC doesn’t support server controls, only option available is using html input controls, so we will be sure about final html rendered at the end. We will also be aware about ‘id‘ of every element. And so integration of ASP.NET MVC application with third party JavaScript libraries like jQuery becomes easy.
      • Support for parallel development - In ASP.NET MVC layers are loosely coupled with each other, so one developer can work on Controller ,at the same time other on View and third developer on Model. This is called parallel development.
      • SEO, URL routing and REST - Rich routing features lets treat every URL as a resource supporting RESTful interfaces.

    Also user-friendly and readable URL improves SEO.

    • Extensibility - ASP.NETMVC supports multiple view engines like aspx, razor and if required we can create our own.
    • Existing ASP.NET Features – ASP.NET MVC framework is built on top of matured ASP.NET framework and thus provides developer to use many good features such as forms authentication, windows authentication, caching, session and profile state management etc.

    Disadvantages:

    • More learning effort - Absence of event driven programming model and ViewState makes ASP.NET MVC a very difficult framework for developers with no or little experience in web application development.

    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 contact[email protected] 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.

    转自:http://www.codeproject.com/Articles/528117/WebForms-vs-MVC

    时间: 2024-11-04 09:28:19

    WebForm vs MVC的相关文章

    WebForm和MVC的一些知识(转)

    转自:http://www.cnblogs.com/liuhf939/p/3417203.html 比较WebForm和Mvc的请求处理方式 首先简单了解一下Asp.Net中怎么对页面进行请求处理的: 在管道的第7-8个事件之间,有一个MapHttpHandler类型,在这个类型的Execute方法中中会通过url去创建一个用于后续处理请求的HttpHandler对象. 判断HttpContext有没有去指向一个具体的HttpHandler处理程序,如果已经指向了一个HttpHandler,那么

    Jquery 上传插件 FineUploader 在 webform 和 mvc 中的使用;

      多文件上传组件FineUploader使用心得 FineUploader 结合 一般处理程序 [上传示例] 参考:http://www.cnblogs.com/dudu/archive/2012/11/27/fine_uploader_mvc_ajax.html  asp.net mvc示例 参考 :http://www.cnblogs.com/chenkai/archive/2013/01/04/2844702.html  http://www.cnblogs.com/guero/p/38

    webform 转 MVC 飞一般的感觉

    前言: 浅谈webform与mvc,让开发变得更加简单,这里主要通过比较webform与mvc的开发方式,以下全属个人看法,不完善的地方可以留言补充. 正文: 废话不多说,直接说工作中经常用到的地方 1.创建页面 webform通过创建.aspx文件来编写前端,.aspx.cs来编写服务端代码,mvc通过创建view来编写前端,Controller里的function来实现服务端(注意:一个控制器里面有多个function,可以控制多个view) 比较:mvc直接贴入前端工程师的代码到view里

    MVC架构、WebForm与MVC对比

    ylbtech-ASP.NET MVC:WebForm与MVC对比 功能描述:WebForm与MVC对比 A.1,MVC架构 •MVC(Model-View-Controller)用于表示一种软件架构模式.它把软件系统分为三个基本部分: –模型(Model) •引用系统数据,管理系统功能并通知View更改用户操作. –视图(View) •就是用户接口,用于显示数据 –控制器(Controller) •将用户操作映射到Model,并操作视图 A.2,3-Tier Architecture(三层架构

    解析ASP.NET WebForm和Mvc开发的区别

    解析ASP.NET WebForm和Mvc开发的区别 2013-12-29 01:59 30052人阅读 评论(9) 收藏 举报  分类: ASP.NET(14)  版权声明:本文为博主原创文章,未经博主允许不得转载. 因为以前主要是做WebFrom开发,对MVC开发并没有太深入的了解.自从来到创新工场的新团队后,用的技术都是自己以前没有接触过的,比如:MVC 和EF还有就是WCF,压力一直很大.在很多问题都是不清楚的情况下,问周围的人,别人也只是给自己讲一个大概.而且前两天因为问了一个比较细的

    WebForm、MVC图片加载失败处理

    还是那个该死的WebFrom项目,部分功能替换为MVC后感觉好多了,但是WebForm.MVC都有图片加载失败时显示提示图片的需求,并且统一在js中处理.问题来了,js中图片路径怎么处理呢?现场有可能部署为网站,也有可能为应用,再加上对路径的不了解,决定弄个IHttpHandler一劳永逸解决路径问题. 搞定了IHttpHandler,发现图片还是显示不出来,日志中多了System.UnauthorizedAccessException: Access to the path 'xxxx' is

    ASP.NET中HttpApplication中ProcessRequest方法中执行的事件顺序;ASP.NET WebForm和MVC整体请求流程图

    ASP.NET中HttpApplication中ProcessRequest方法中执行的事件顺序 1.BeginRequest  开始处理请求 2.AuthenticateRequest 授权验证请求,获取用户授权信息 3.PostAuthenticateRequest 获取成功 4.AunthorizeRequest 授权,一般来检查用户是否获得权限 5.PostAuthorizeRequest 获得授权 6.ResolveRequestCache 获取页面缓存结果(如果没有则执行) 7.Po

    [转]asp.net webform 与mvc 共享session

    公司内部系统最早是用.net webform模式开发的,现新项目用.net mvc 开发,现存在的问题就是如何保持原有.net webform的登录状态不变,而在mvc中能够验证用户的登录状态,也就是将.net webform 中session中保存的登录状态共享给mvc. 在cnblogs中搜索相关资料,基本解决方法都是将session状态保存在数据库中,然后原有程序和新程序都获取数据库中的session状态 ,从而实现用户登录状态信息共享. 具体实现步骤如下: 1.创建session 保存的

    FusionCharts simple demo for (html+js、APS.NET Webform、MVC)

    做GIS或其他内部数据统计项目的应该对FusionCharts也不会太陌生,简单易用已无需多说什么了,只是有时候框架不同,实现起来也稍有差异 引用dll调用FusionCharts类的静态方法RenderChartHTML 返回html绑定在数据控件上更为符合webform: 使用JS代码new FusionCharts对象,调用对象的setDataXML或者setDataURL方法更为符合MVC 1.HTML+JS <!--html--> <html xmlns="http:

    WebForm与MVC混用

    步骤一:添加引用 -> 程序集 -> 扩展 -> System.Web.Mvc ; System.Web.Razor; System.Web.WebPages; System.Web.Abstractions; 步骤二:mvc默认目录结构 新建一个空的mvc项目, 将Controllers和Views文件夹全部拷贝过来(文件结构和web.config这个很重要) 步骤三:修改配置文件web.config(不是Views文件夹下的) 1.在compilation节点下加入以下几个assem