ASP.NET MVC Framework是微软官方提供的MVC模式(深入MVC模式概念)编写ASP.NET Web应用程序的一个框架.已于2009年3月19日正式发布了Asp.net MVC 1.0。
个人对Asp.net WebForm的熟悉程度远远大于Asp.net MVC,但最近学习了Asp.net MVC,感觉到了一种新颖的开发模式,和WebFroms开发有很大的不同。那么我们应该怎样的看待这两种开发模式呢?以及我们应该怎么进行取舍?
一、Asp.net MVC是什么?
Asp.net MVC是一个基于微软发布的MVC的框架,目的和Webforms是一样的,可以高效快速的开发Web应用程序。那它和webforms有什么冲突呢?用Microsoft来话来说,Asp.net MVC并不是Webforms的替换,也不是互补,要么用Asp.net MVC,要么用Asp.net WebForms。但在某些场景下asp.net MVC比asp.net Webform要更有优势,但是在另外一些情况下:asp.net webform要更适合一些。
二、Asp.net MVC产生
在Asp.net MVC出现之前,已经有很多MVC框架,很多开发人员觉得MVC框架更适合做Web开发。而且很多的asp.net开发人员都不愿意使用服务器控件。所以微软决定自己开发一套属于自己的MVC框架。
三、Asp.net WebForms特点
优点:拖控件,和window form一样,快速,高效的开发。
缺点:
1.服务器控件自动的产生自己相关的HTML的代码,在一定的程度上是控制不了,而且很容易发生命名冲突。
2.ViewState是Client和Server保存视图状态的一种方式,但是影响性能,而且当ViewState太大的时候,会引起错误。
3.自动生成的ID是不可控制的。所以用Javascript增加它的行为很困难。
4.对单元测试支持不好。
四、Asp.net MVC Framework
优点:
1.Clear separation of concerns
2.Testability-support for Test-Dirven Development
3.Fine-grained control over HTML and Javascript
4.Intuitive URLs(友好的URL)
5.Extensible to adapt to you unique needs(扩展能力强)
缺点:
1.Require developers to have more html,css and javascript knowledge.
2.write more code,both server-side and client-side
(1)Controlller(with Action,Action Result)
Controllers process incoming request,handle user input and interactions,and excute appropriate application logic.A controller class typically calls a
separate view component to generate the HTML markup for the request.
User interaction with Asp.net MVC application is organized around controller and action method.the controller defines action methods.
ActionResult is what an Action returns,it could be a view result,a redirect,etc.
(2)Model
Your business logic and your data access goes here.
(3)View
Aspx is used to render views.
View should not have any business logic
Controller passes to view the data required for rendering,no more,no less.
(4)URL Routing
URLRoutingModule is the responsible for URL routings.
URL routing is based on patterns
(5)Helpers
Helpers are basically objects to help us generate code,be it HTML,CSS,Javascript.
Helpers are mainly used to help us type less code.
HTML.ActionLink(...)
(6)Action Filters
A way to intercept action method calls,works like AOP.
Server build-in action filters are provided by default.
you can create your own very easily
Sample:Authorize OutputCache
(7)Model Binding
It's a way of structuring input data,rather than reading all input data from the Request object collection.
(8)Form Validation
You can error message related to model throuth the viewData.Modelstatus
you output these message with the HTML Helper mothod HTML.ValidationMessage.
如果有什么理解不正确或表达得不清楚,希望朋友们提出来了。非常感谢!
Best Regards,
Charles Chen