http://www.fluidea.cn/blog/index.php/2007/11/12/36/
此文是我翻译自OReilly出版的 Programming Flex 2 中部分章节,略有修改.
从某种程度上说,即使一点也不了解Flex程序的运行机制,也并不妨碍我们开发Flex程序。不过,弄清楚那些基本的结构还是非常有好处的,起码可以知道它的内部是怎么工作的。这会帮助我们实现一些有趣的功能,比如个性化预加载条,在运行期间加载其它的Flex程序,管理运行期间库元件的加载和卸载等等。更远的方面,理解Flex程序的运行可以帮助完善程序,因为我们将知道如何去优化代码。比如,如果想确定哪些代码是在预加载期间执行的,就得知道如何去捕捉这些事件。
每个Flex程序都是从SystemManager开始的,它是flash.display.MovieClip的子类,一个可视的数据类型, MovieClip支持时间轴。SystemManager有两桢。Flex程序的第一桢用来显示一个下载进度条。这一桢应该尽量使用少量的元素,避免文件尺寸过大,使得可以被快速下载,这时候,Flex框架还有大部分没有加载,只有一些核心的类库被加载,加载结束后,进入第二桢。直到第二桢,程序才完全加载进来,包括程序中使用的Flex框架中的大部分类库。如果我们要个性化预加载条,就得先了解SystemManager的工作流程。
下图演示了程序的初始化流程:

SystemManager的初始化流程
一旦程序中SystemManager的实例进入第二桢,它将创建主程序类的一个实例。SystemManager实例有个属性 “application”,初识值为null,直到此时才被指向主程序实例。然后,主程序的实例开始自身的初始化。这时,依次触发了主程序的内部事件:
preinitialize
表示程序被初始化了但还没有开始创建任何子对象.
initialize
程序创建了子对象但这些子对象还没有调整好布局
creationComplete
程序完成了初始化,并绘制好了所有子对象。
主程序完成了这一切后,抛出applicationComplete 事件,并告诉SystemManager :一切准备完毕,可以开始运行了.
SystemManager也负责管理所有位于程序显示层次最顶层的对象,包括所有的pop up(弹出窗口),cursors(鼠标样式),和tool tips(鼠标提示)都位于SystemManager实例中。
SystemManager的属性“topLevelSystemManager”,代表当前运行在Flash Player中的根元素。在Flex程序中,这个属性总是指向SystemManager实例自身。当一个Flex程序被加载到另一个Flex程序中时,被加载的程序也会拥有自己的SystemManager,但这时候它的topLevelSystemManager属性就会指向主程序的 SystemManager对象。
一般情况下,我们并不需要去使用SystemManager对象,但如果想使用它,也是非常容易的,所有UIComponents的子类(包括Application)都有一个“systemManager”属性,指向当前的SystemManager对象。
在实际开发中,我们可能会使用SystemManager去侦听程序中任何一个可视对象的事件,因为事件冒泡机制告诉我们,冒泡阶段的最后一个对象总是SystemManager。
接下来的内容是:加载另一Flex程序,待续
Flex学习曲线
转自:http://www.51as.com/flex/2007-11-6/ZenMeXueXi-flex.html
原来有人问我:怎样学好Flash?我的回答一般就是:仔细看帮助、多做练习、多看优秀的源码、多上专业论坛参加讨论。
可是Flex来了,于是又有人问:怎样学好Flex?
我不知如何回答,因为我也是Flex新手,也在“仔细看帮助、做练习、看源码、上论坛……”。现在d.CAT的这篇优秀的文章,详细的回答了这个问题。
下面的文章转自d.CAT RIA Blog,由于原文是繁体中文的,所以转载过来的时候我对文章的繁体字部分进行了替换,对一些词语进行了修改以符合简体中文语言习惯,对一些术语进行了注释。
最后,文中所有第一人称处所指的都是原文作者而不是“我”,有麻烦可以找他 :em61:
以下为转载:
==================================================================
*Flex 的基础架构
关于 flex 基本上常被问到的不外乎就是“如何可以学好它?”,要了解这个问题的答案基本上只要看懂下面这个图就OK了。
Flex 的基础架构
*Actionscript 该学的重点
从最底层看起,最下面的 actionscript 3是一切的基础,它是 flash/flex 编程使用的唯一程式语言,因此任何人想学好 flex 第一件事绝对是先摸熟 actionscript 这个语言,包含:
1. 它的基本语法与结构(array, hash, loop, if else…)
2. DisplayList (DisplayObject, DisplayObjectContainer)与 Event system(bubbling, propagating…)
3. Sound, Video, NetConnection 与 Graphics class
掌握 as3 的精华后,接下来就可以进入 flex framework。
*Flex framework 的重点
基本上 flex framework 就是用 actionscript 写成的框架,因此也可以把它看成是 as3的最好示范,看着 framework source 学 actionscript 也是挺不错的,只是路会变很长。
Flex Framework 整个体系非常博大精深,通常一般人不太可能完整把它学完,只需要针对最常用到的部份熟悉就好,图中列出的那三块(component, managers, style/skin)就是我个人认为所有初学者最优先该学会的。
*Component 该学些什么
Component 是整个 flex framework 的基础,几乎80% 的元素都是由 UIComponent 继承而来,例如最根本的它本身就是一个 UIComponent,因此,熟悉 component 就成为学好 flex framework 最根本也最重要的基本功
Flex 内建了 二十几个 UI controls, 例如 Button, DataGrid, HBox等,以种类来分,这些 components 可以概分为三大类:
* Controls: Button, DateChooser, Slider…
* Containers: Box, DividedBox, Panel…
* List: DataGrid, Tree, TileList…
初学者第一步至少该学会怎么用这些元件,了解每个元件的 properties, events, styles, effects…,知道怎么在手册里查它的 API 文件,以及何时该用何种元件。
进一步,则是学会怎么修改这些元件,例如继承一个 Button 下来加上不同的功能,或是写不同的 skin border 来改变它的外观。
再进一步,则是开始研究元件的生命周期,了解每个元件是何时初始化,元件内部有那些关键指令与它们个别的功能,然后可以试着建立自已的 custom component。
这一关看起来容易但实际上最困难,因为 flex 的 component framework 写的非常庞大,虽然乱中有序但要在混沌中看出隐藏的架构然后抓住重点整串提起,就非得有人带着指引正确的途径才比较可能完成。
*manager 是什么
图中最上方的第二块就是 manager。
flex 里有很多的 managers,负责做各种不同的工作(废话…),几个比较重要的包含:
* SystemManager:
它是每个 flex 程序的根源,最先被下载,也最早启动,由它进行一连串的 app boot流程
* StyleManager:
它负责整支app 的 css style 套用与 skin 生成,如果想玩动态 css 载换也靠它
* DragManager:
Flex 最大的卖点就是 drag and drop(拖放),这个 manager 就是背后的英雄,初学者至少要学会怎么处理 drag 行为的五个事件,以及如何在不同元件间做拖放;进阶的玩家则应该深入研究这支 manager 是怎么写成的,详细阅读它的 source 会得到意想不到的无穷乐趣(如果你读完却没有这种感觉,呃,那代表你该再多读几次,如果还是没有,那请私下联络我 😀 )
* ModuleManager:
使用 Flex 开发大型应用程式时,往往会将程式切割成许多小的 module, 这个 manager 就是负责载入并管理所有的 module (包含它的 class partition),初心者或许用不到,但有志深入的玩家一定要很熟。
* CursorManager:
这个用到的时机不是很多,但偶尔要换一下 cursor 时还是会用到,初学者至少要知道怎么用指定的图案去换掉系统cursor。
*Style/Skin 的重点
CSS style 与 skinning 是 Flex 最大的卖点之一,也是开发过程中较为麻烦也最耗时的部份。
初学者应该要彻底了解如何使用 CSS style 来打点一支 flex app 的外观,换颜色、素材,使用外部 assets 修饰介面。
中阶玩家则应该了解 skinning 的系统,包含 programmatic skinning 与 graphical skin,它们俩的差别?使用时机?如何客制化(zrong注1)?
更高阶的玩家则应该熟悉整个 Styling system 的运作模式,外加如何动态载入 css 在 runtime 换掉整个介面。
简而言之,flex app 写的好不好,外行人其实看不太出来,但一支 app UI 美不美则是一翻两瞪眼,比较漂亮的那就先加十分
(当然,有一种情况是刻意用心去美化了介面结果弄巧成拙搞的怨声载道人人喊打,但那种比较不多见,也不是每家公司都会搞到这步田地,就先不讨论)
*学完基本功后下一步
在我的标准里,当一个 developer 对上图内每一块都有中等程度的了解后,就算是完成 flex 养成教育,可以迈向下一个阶段。
也就是开始熟悉 application 的制作手法,这包含
* 了解至少一种以上的开发框架,例如 Cairngorm,老实说我对这个框架没什么好感(因为手法太复杂,只适合超复杂登月计画或火星探勘时使用),但它结构设计良好,又是业界公认的圣杯,等于是专家们共通的语言,因此至少要先了解它在做什么,将来在专业场合才好沟通(俗话说 know the rules so you know what you are breaking, 就是指这情况)
* 接着可以看看比较简单的手法,像 Riawave, Model-Glue:Flex, PureMVC…等,基本上这些框架设计方式都大同小异,每个都有不同的应用场合,可以挑一个喜欢的再自行修改。
* 了解基本的概念,例如 Value Object, DAO, MVC 等,它们在大部份的程式框架里都会出现,早点学会日子比较轻松。
* 接着就是开始实际 coding,写一个中小型规模的app,不论是单纯的 CRUD (zrong注2)程序,或是留言版、电话簿、进销存管理都可以,籍由多写来强化编程的概念,然后透过大量的 peer code review 来找出可改进的地方。
*结论
结论还是老话一句:要入门 flex 超级简单,只要不是白痴应该一小时就行,但要成为可独当一面的专业开发者,路就很长,如果没有走对方向很容易就迷失甚至最后放弃。
换句话说,要能成为职场上真正需要的 professional developer,并不如表面上想象的容易(其实我想每种技术领域跟产业都一样吧),这也是我过去半年来协助很多公司做 recruiting 后的感想。
zrong注1:按客人要求不同定义
zrong注2:CRUD是指在做计算处理时的增加、查询(重新得到数据)、更新和删除(create, retrieve, update, and delete)几个单词的首字母简写。主要被用在描述软件系统中数据库或者持久层的基本操作功能
Flex学习站
今天把收藏夹共享出来,希望对学习Flex的人有所帮助。
一、国外站点
1.资源类
Adobe Flex 2 Component Explorer: 官方的,展示了各种组件的用法,入门必看。
CFlex:很好的一个Flex资源站点,包括教程,新闻,资源站点…… 只是页面有点杂乱,大家一般看右边那一栏就行了。
FlexBox:一个收集了网上很多开源组件的站点,是进阶学习的好帮手。
FlexLib:也是一个开源Flex组件站点,不过与FlexBox不同的是,这个是原创,而FlexBox只是收集。
Flex Developer Center:Adobe Flex开发者中心,经常会有一些好的教程出现。
Adobe Labs:这个不用我说了吧。
Flex.org:http://www.flex.org/ 官方的,基本上应有尽有。
2. Explorers
Flex 2 Style Explorer:用来设计程序样式风格的工具,很好用,现在源代码已经可以下载。
Flex 2 Primitive Explorer:用来调节各种Primitive图形的组件,非官方的,源代码提供下载。
Flex 2 Filter Explorer:用来调节各种滤镜(filter),非官方的,源代码提供下载。
3. Blogs
MXNA:这个不用我说了吧,虽说这不是一个Blog,但是它聚合了所有优秀的Blog,所以把它放在Blog一栏,下面所有的Blog都在这个聚合中。
Alex Uhlmann:http://weblogs.macromedia.com/auhlmann/
Christophe Coenraets:http://coenraets.org/ 特别推荐
Code Slinger:http://blogs.digitalprimates.net/codeSlinger/
Deitte:http://www.deitte.com/
Doug mccune:http://dougmccune.com/blog/ 特别推荐
Flex Doc Team:http://blogs.adobe.com/flexdoc/
Kuwamoto:http://kuwamoto.org/ 特别推荐
Macromedia Consulting:http://weblogs.macromedia.com/mc/
Matt Chotin:http://weblogs.macromedia.com/mchotin/ 特别推荐
Peter Ent:http://weblogs.macromedia.com/pent/ 特别推荐
Quietly Scheming:http://www.quietlyscheming.com/blog/ 特别推荐
ScaleNine Blog:http://www.scalenine.com/blog/index.php 特别推荐
Steven Webster:http://weblogs.macromedia.com/swebster/
EverythingFlex:http://blog.everythingflex.com/ 特别推荐
Alex’s Flex Closet:http://blogs.adobe.com/aharui/ 特别推荐
4. 邮件列表
FlexCoders:http://tech.groups.yahoo.com/group/flexcoders/
Flex Components:http://www.adobe.com/go/flexcomponents 非高级开发者最好别加入
上面是两个比较有名的邮件列表,建议大家提问之前先搜索一下邮件存档,一般都能找到答案,找不到再提问。更多邮件列表请看这里:http://flex.org/community/
5.Cairngorm 相关
Cairngorm Documentation Group 这个里面收集了基本上所有关于Cairngorm的资料
二、国内站点
1.论坛
RIACHINA:前身是RIACN,国内最好的Flex论坛之一。我最初知道Flex从这里开始,对这个站挺有感情,饮水思源,把它排第一。
AnyFlex:国内最好的Flex论坛之一,成立的时间比较早,而且论坛FTP中有很多好的资料。
RIADev:Google网上论坛,d.CAT前辈主持的,一般小问题都能解决。
FlexCoders.cn:刚起步的论坛,不过看域名觉得挺有前途,呵呵。
2.Blogs
Dreamer's Blog:就是本站。我翻译了国外Flex Blog上的大量优秀文章,自认为是国内中文资料最多的站点之一。
Y.X.Shawn:对Flex研究很深入,自己写一些开源的组件。
d.CAT:高级开发者,台湾的,为数不多的华语高级开发者,他还做过一个类似Caringorm的架构。
Kenshin:很早就开始研究Flex了,自己开发过很多东西。
3.Cairngorm
没有。不过我翻译过一个关于Cairngorm 小文档,大概30页左右,或许对你有帮助。您可以在AnyFlex 论坛下载到
友情提示:上面这些站点中,资源类的更新不快,不用天天看;Blog和MXNA值得天天看,当然您也可以关注本站,因为我会把MXNA上的关于Flex的内容整理过来;有问题请先去邮件列表或者论坛中搜索,基本上都能搜索到。
writed by bonashen
MSDN面向服务的面向业务基础
微软MSDN中的面向服务介绍
地址:
http://www.microsoft.com/china/MSDN/library/architecture/SOADesign.mspx?mfr=true
ASP抓取Flash中的图片组件
点击下载此文件
IronSoft ASP系列组件里面包含着四个组件:文件上传组件,图片处理组件,Flash抓图组件, Gif动画处理组件。原生的Win32组件,让它们运行起来特别有效率。
文件上传组件:支持上G级的大文件上传,同时使用磁盘缓存,可以最少占用系统的内存资源。是企业局域网内大文件上传的完美解决方案。
图片处理组件:现在已经支持BMP,GIF(非动画,透明),JPG三种网上主流的图片格式,可以高效地进行复杂的图片处理,格式转换,制作水印等。支持简单的滤镜(灰度和反转),以及支持JPEG的EXIF信息提取。
Flash抓图组件:国内首创的Flash组件,可以对FLASH的指定帧进行抓图,是制作Flash欣赏网站的不可缺少的助手。
GIF动画处理组件:支持GIF动画的创建,缩放,添加删除帧,单独提取指定帧里的图像等。
在asp文件中访问flash详细信息
<%
Class SWFDump
Private header
Private RECTdata
Private nBits
Private mversion
Private mfilelen
Private mxMin
Private mxMax
Private myMin
Private myMax
Private mheigt
Private mwidth
Private mframerate
Private mframecount
Private Sub Class_Initialize()
End Sub
Private Sub Class_Terminate()
End Sub
Private Function ReadHeader (filename)
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Dim fso, f
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile(filename, ForReading)
ReadHeader = f.Read(21)
End Function
Private Function ToBin(inNumber, OutLenStr )
Dim binary
binary = ""
do while inNumber >= 1
binary = binary & inNumber mod 2
inNumber = inNumber \ 2
loop
binary = binary & String(OutLenStr - len(binary), "0")
ToBin = StrReverse(binary)
End Function
Private Function Bin2Decimal(inBin)
Dim counter
Dim temp
Dim Value
inBin = StrReverse(inBin)
temp = 0
For counter = 1 to Len(inBin)
If counter = 1 then
Value = 1
Else
Value = Value * 2
End If
temp = temp + mid(inBin, counter ,1) * Value
Next
Bin2Decimal = temp
End Function
Public Function SWFDump(fileName)
header = ReadHeader (fileName)
mversion = asc(mid(header,4,1))
mfilelen = asc(mid(header,5,1))
mfilelen = mfilelen + asc(mid(header,6,1)) * 256
mfilelen = mfilelen + asc(mid(header,7,1)) * 256 * 256
mfilelen = mfilelen + asc(mid(header,8,1)) * 256 * 256 * 256
RECTdata = ToBin(asc(mid(header,9,1)),8)
RECTdata = RECTdata & ToBin(asc(mid(header,10,1)),8)
RECTdata = RECTdata & ToBin(asc(mid(header,11,1)),8)
RECTdata = RECTdata & ToBin(asc(mid(header,12,1)),8)
RECTdata = RECTdata & ToBin(asc(mid(header,13,1)),8)
RECTdata = RECTdata & ToBin(asc(mid(header,14,1)),8)
RECTdata = RECTdata & ToBin(asc(mid(header,15,1)),8)
RECTdata = RECTdata & ToBin(asc(mid(header,16,1)),8)
RECTdata = RECTdata & ToBin(asc(mid(header,17,1)),8)
nBits = Mid(RECTdata,1,5)
nBits = Bin2Decimal(nBits)
mxMin = Bin2Decimal(Mid(RECTdata,6,nBits))
mxMax = Bin2Decimal(Mid(RECTdata,6 + nBits * 1 ,nBits))
myMin = Bin2Decimal(Mid(RECTdata,6 + nBits * 2 ,nBits))
myMax = Bin2Decimal(Mid(RECTdata,6 + nBits * 3 ,nBits))
mheigt = (myMax - myMin) / 20
mwidth = (mxMax - mxMin) / 20
mframerate = asc(mid(header,18,1))
mframecount = asc(mid(header,19,1))
mframecount = mframecount + asc(mid(header,20,1)) * 256
End Function
Public Property Get Heigt()
Heigt = mheigt
End Property
Public Property Get Width()
Width = mwidth
End Property
Public Property Get Version()
Version = mversion
End Property
Public Property Get FileLen()
FileLen = mfilelen
End Property
Public Property Get xMin()
xMin = mxMin
End Property
Public Property Get xMax()
xMax = mxMax
End Property
Public Property Get yMin()
yMin = myMin
End Property
Public Property Get yMax()
yMax = myMax
End Property
Public Property Get Framerate()
Framerate = mframerate
End Property
Public Property Get Framecount()
Framecount = mframecount
End Property
End Class
%>
做成包含文件swfheaderdump.inc
调用:
<!-- #include file="swfheaderdump.inc" -->
<%
' Pass the SWF name in querystring this way
' swfdump.asp?swf=yourmovie.swf
set myObj = new swfdump
myObj.SWFDump (Server.MapPath(request("swf")))
Response.Write "Heigt (pixel) = " & myObj.Heigt & "<br>"
Response.Write "Width (pixel) = " & myObj.Width & "<br>"
Response.Write "Version = " & myObj.Version & "<br>"
Response.Write "FileLen (bytes) = " & myObj.FileLen & "<br>"
Response.Write "xMin (twips) = " & myObj.xMin & "<br>"
Response.Write "xMax (twips) = " & myObj.xMax & "<br>"
Response.Write "yMin (twips) = " & myObj.yMin & "<br>"
Response.Write "yMax (twips) = " & myObj.yMax & "<br>"
Response.Write "FrameRate = " & myObj.FrameRate & "<br>"
Response.Write "FrameCount = " & myObj.FrameCount & "<br>"
%>
Flash SWF 文件性能优化
flash避免占用CPU资源过度的几种方法
最近做网站时候,发现首页打开后一段时间,客户端的IE进程会出现CPU资源100%甚至内存不断增长导致客户端死机的问题,一直以为是JavaScript脚本在进行死循环插入html造成,页面文件不断增加,结果屏蔽了所有JavaScript的脚本,还是无济于事,又仔细思考了一下原因,首先从web页面的原理来说,第一次访问主页,可能需要客户端资源进行与服务器间的交互,然后服务器端响应客户端请求,执行服务器端代码,生成静态的html网页代码,然后返回给客户端浏览器解析html显示,浏览器解析后,显示完毕,客户端的内存和cpu就不应该再增长了,分析了原理,于是对页面进行了审视,发现页面上除了脚本的滚动字幕效果外,就是那些花花绿绿的flash动画是动的东西了,于是将script滚动和flash全部注释掉,再次访问主页,内存和cpu就此定格,然后将script的滚动解禁了,依然不会持续占用资源,那么只有一个可能就是flash!于是搜索了一下flash动画优化方法,特转贴:
1.减少本透明物体的使用率
2.减少大型图片的移动,这个是最最耗资源的,罪魁祸首!
3.减少大型元件的复制
4.压缩声音的输出品质
5.将大型位图转换成矢量图
6.将部分mc单独输出成swf文件,然后使用load movie导入
7.实在不行的话降低swf的品质。
用户喜爱的10个AIR应用
http://www.readwriteweb.com/archives/10_adobe_air_apps_bloggers_will_love.php
另外的消息:
未来两天,Adobe Media Player正式版就要发布了。Flash Player 9.4也将会很快出现。
Flash Media Server 3.0.1已经出现在Adobe support专区。
ASP.NET MVC Framework
One of the things that many people have asked for over the years with ASP.NET is built-in support for developing web applications using a model-view-controller (MVC) based architecture.
Last weekend at the Alt.NET conference in Austin I gave the first public demonstration of a new ASP.NET MVC framework that my team has been working on. You can watch a video of my presentation about it on Scott Hanselman's blog here.
We'll be releasing a public preview of this ASP.NET MVC Framework a little later this year. We'll then ship it as a fully supported ASP.NET feature in the first half of next year.
What is a Model View Controller (MVC) Framework?
MVC is a framework methodology that divides an application's implementation into three component roles: models, views, and controllers.
* “Models” in a MVC based application are the components of the application that are responsible for maintaining state. Often this state is persisted inside a database (for example: we might have a Product class that is used to represent order data from the Products table inside SQL).
* “Views” in a MVC based application are the components responsible for displaying the application's user interface. Typically this UI is created off of the model data (for example: we might create an Product “Edit” view that surfaces textboxes, dropdowns and checkboxes based on the current state of a Product object).
* “Controllers” in a MVC based application are the components responsible for handling end user interaction, manipulating the model, and ultimately choosing a view to render to display UI. In a MVC application the view is only about displaying information – it is the controller that handles and responds to user input and interaction.
One of the benefits of using a MVC methodology is that it helps enforce a clean separation of concerns between the models, views and controllers within an application. Maintaining a clean separation of concerns makes the testing of applications much easier, since the contract between different application components are more clearly defined and articulated.
The MVC pattern can also help enable red/green test driven development (TDD) – where you implement automated unit tests, which define and verify the requirements of new code, first before you actually write the code itself.
A few quick details about the ASP.NET MVC Framework
I'll be doing some in-depth tutorial posts about the new ASP.NET MVC framework in a few weeks once the bits are available for download (in the meantime the best way to learn more is to watch the video of my Alt.net presentation).
A few quick details to share in the meantime about the ASP.NET MVC framework:
* It enables clean separation of concerns, testability, and TDD by default. All core contracts within the MVC framework are interface based and easily mockable (it includes interface based IHttpRequest/IHttpResponse intrinsics). You can unit test the application without having to run the Controllers within an ASP.NET process (making unit testing fast). You can use any unit testing framework you want to-do this testing (including NUnit, MBUnit, MS Test, etc).
* It is highly extensible and pluggable. Everything in the MVC framework is designed so that it can be easily replaced/customized (for example: you can optionally plug-in your own view engine, routing policy, parameter serialization, etc). It also supports using existing dependency injection and IOC container models (Windsor, Spring.Net, NHibernate, etc).
* It includes a very powerful URL mapping component that enables you to build applications with clean URLs. URLs do not need to have extensions within them, and are designed to easily support SEO and REST-friendly naming patterns. For example, I could easily map the /products/edit/4 URL to the “Edit” action of the ProductsController class in my project above, or map the /Blogs/scottgu/10-10-2007/SomeTopic/ URL to a “DisplayPost” action of a BlogEngineController class.
* The MVC framework supports using the existing ASP.NET .ASPX, .ASCX, and .Master markup files as “view templates” (meaning you can easily use existing ASP.NET features like nested master pages, <%= %> snippets, declarative server controls, templates, data-binding, localization, etc). It does not, however, use the existing post-back model for interactions back to the server. Instead, you'll route all end-user interactions to a Controller class instead – which helps ensure clean separation of concerns and testability (it also means no viewstate or page lifecycle with MVC based views).
* The ASP.NET MVC framework fully supports existing ASP.NET features like forms/windows authentication, URL authorization, membership/roles, output and data caching, session/profile state management, health monitoring, configuration system, the provider architecture, etc.
Summary
If you are looking to build your web applications using a MVC approach, I think you'll find this new ASP.NET MVC Framework option very clean and easy to use. It will enable you to easily maintain separation of concerns in your applications, as well as facilitate clean testing and TDD.
I'll post more tutorials in the weeks ahead on how the new MVC features work, as well as how you can take advantage of them.
Hope this helps,
Scott

Two weeks ago I blogged about a new MVC (Model View Controller) framework for ASP.NET that we are going to be supporting as an optional feature soon. It provides a structured model that enforces a clear separation of concerns within applications, and makes it easier to unit test your code and support a TDD workflow. It also helps provide more control over the URLs you publish in your applications, and can optionally provide more control over the HTML that is emitted from them.
Since then I've been answering a lot of questions from people eager to learn more about it. Given the level of interest I thought it might make sense to put together a few blog posts that describe how to use it in more detail. This first post is one of several I'll be doing in the weeks ahead.
A Simple E-Commerce Storefront Application
I'm going to use a simple e-commerce store application to help illustrate how the ASP.NET MVC Framework works. For today's post I'll be implementing a product listing/browsing scenario in it.
Specifically, we are going to build a store-front that enables end-users to browse a list of product categories when they visit the /Products/Categories URL on the site:

When a user clicks on a product category hyperlink on the above page, they'll navigate to a product category listing URL – /Products/List/CategoryName – that lists the active products within the specific category:

When a user clicks an individual product, they'll navigate to a product details URL – /Products/Detail/ProductID – that displays more details about the specific product they selected:

We'll build all of the above functionality using the new ASP.NET MVC Framework. This will enable us to maintain a “clean separation of concerns” amongst the different components of the application, and enable us to more easily integrate unit testing and test driven development.
Creating A New ASP.NET MVC Application
The ASP.NET MVC Framework includes Visual Studio Project Templates that make it easy to create a new web application with it. Simply select the File->New Project menu item and choose the “ASP.NET MVC Web Application” template to create a new web application using it.
By default when you create a new application using this option, Visual Studio will create a new solution for you and add two projects into it. The first project is a web project where you'll implement your application. The second is a testing project that you can use to write unit tests against it:

You can use any unit testing framework (including NUnit, MBUnit, MSTest, XUnit, and others) with the ASP.NET MVC Framework. VS 2008 Professional now includes built-in testing project support for MSTest (previously in VS 2005 this required a Visual Studio Team System SKU), and our default ASP.NET MVC project template automatically creates one of these projects when you use VS 2008.
We'll also be shipping project template downloads for NUnit, MBUnit and other unit test frameworks as well, so if you prefer to use those instead you'll also have an easy one click way to create your application and have a test project immediately ready to use with it.
Understanding the Folder Structure of a Project
The default directory structure of an ASP.NET MVC Application has 3 top-level directories:
* /Controllers
* /Models
* /Views
As you can probably guess, we recommend putting your Controller classes underneath the /Controllers directory, your data model classes underneath your /Models directory, and your view templates underneath your /Views directory.
While the ASP.NET MVC framework doesn't force you to always use this structure, the default project templates use this pattern and we recommend it as an easy way to structure your application. Unless you have a good reason to use an alternative file layout, I'd recommend using this default pattern.
Mapping URLs to Controller Classes
In most web frameworks (ASP, PHP, JSP, ASP.NET WebForms, etc), incoming URLs typically map to template files stored on disk. For example, a “/Products.aspx” or “/Products.php” URL typically has an underlying Products.aspx or Products.php template file on disk that handles processing it. When a http request for a web application comes into the web server, the web framework runs code specified by the template file on disk, and this code then owns handling the processing of the request. Often this code uses the HTML markup within the Products.aspx or Products.php file to help with generating the response sent back to the client.
MVC frameworks typically map URLs to server code in a different way. Instead of mapping URLs to template files on disk, they instead map URLs directly to classes. These classes are called “Controllers” and they own processing incoming requests, handling user input and interactions, and executing appropriate application and data logic based on them. A Controller class will then typically call a separate “View” component that owns generating the actual HTML output for the request.

The ASP.NET MVC Framework includes a very powerful URL mapping engine that provides a lot of flexibility in how you map URLs to Controller classes. You can use it to easily setup routing rules that ASP.NET will then use to evaluate incoming URLs and pick a Controller to execute. You can also then have the routing engine automatically parse out variables that you define within the URL and have ASP.NET automatically pass these to your Controller as parameter arguments. I'll be covering more advanced scenarios involving the URL routing engine in a future blog post in this series.
Default ASP.NET MVC URL Routing to Controller Classes
By default ASP.NET MVC projects have a preconfigured set of URL routing rules that enable you to easily get started on an application without needing to explicitly configure anything. Instead you can start coding using a default set of name-based URL mapping conventions that are declared within the ASP.NET Application class of the Global.asax file created by the new ASP.NET MVC project template in Visual Studio.
The default naming convention is to map the leading URL path of an incoming HTTP request (for example: /Products/) to a class whose name follows the pattern UrlPathController (for example: by default a URL leading with /Products/ would map to a class named ProductsController).
To build our e-commerce product browsing functionality, we'll add a new “ProductsController” class to our project (you can use the “Add New Item” menu in Visual Studio to easily create a Controller class from a template):

Our ProductsController class will derive from the System.Web.MVC.Controller base class. Deriving from this base class isn't required – but it contains some useful helper methods and functionality that we'll want to take advantage of later:

Once we define this ProductsController class within our project, the ASP.NET MVC framework will by default use it to process all incoming application URLs that start under the “/Products/” URL namespace. This means it will be automatically called to process the “/Products/Categories”, “/Products/List/Beverages”, and “/Products/Detail/3” URLs that we are going to want to enable within our store-front application.
In a future blog post we'll also add a ShoppingCartController (to enable end users to manage their shopping carts) and an AccountController (to enable end users to create new membership accounts on the site and login/logout of it). Once we add these two new controller classes to our project, URLs that start with /ShoppingCart/ and /Account/ will automatically be routed to them for processing.
Note: The ASP.NET MVC framework does not require that you always use this naming convention pattern. The only reason our application uses this by default is because there is a mapping rule that configures this that was automatically added to our ASP.NET Application Class when we created the new ASP.NET MVC Project using Visual Studio. If you don't like this rule, or want to customize it to use a different URL mapping pattern, just go into the ASP.NET Application Class (in Global.asax) and change it. I'll cover how to-do this in a future blog post (when I'll also show some of the cool scenarios the URL routing engine enables).
Understanding Controller Action Methods
Now that we have a created a ProductsController class in our project we can start adding logic to handle the processing of incoming “/Products/” URLs to the application.
When defining our e-commerce storefront use cases at the beginning of this blog post, I said we were going to implement three scenarios on the site: 1) Browsing all of the Product Categories, 2) Listing Products within a specific Category, and 3) Showing Details about a Specific Product. We are going to use the following SEO-friendly URLs to handle each of these scenarios:
URL Format Behavior URL Example
/Products/Categories Browse all Product Categories /Products/Categories
/Products/List/Category List Products within a Category /Products/List/Beverages
/Products/Detail/ProductID Show Details about a Specific Product /Products/Detail/34
There are a couple of ways we could write code within our ProductsController class to process these three types of incoming URLs. One way would be to override the “Execute” method on the Controller base class and write our own manual if/else/switching logic to look at the incoming URL being requested and then execute the appropriate logic to process it.
A much easier approach, though, is to use a built-in feature of the MVC framework that enables us to define “action methods” on our controller, and then have the Controller base class automatically invoke the appropriate action method to execute based on the URL routing rules in use for our application.
For example, we could add the below three controller action methods to our ProductsController class to handle our three e-commerce URL scenarios above:

The URL routing rules that are configured by default when a new project is created treat the URL sub-path that follows the controller name as the action name of the request. So if we receive a URL request of /Products/Categories, the routing rule will treat “Categories” as the name of the action, and the Categories() method will be invoked to process the request. If we receive a URL request of /Products/Detail/5, the routing rule will treat “Detail” as the name of the action, and the Detail() method will be invoked to process the request, etc.
Note: The ASP.NET MVC framework does not require that you always use this action naming convention pattern. If you want to use a different URL mapping pattern, just go into the ASP.NET Application Class (in Global.asax) and change it.
Mapping URL Parameters to Controller Action Methods
There are several ways to access URL parameter values within the action methods of Controller classes.
The Controller base class exposes a set of Request and Response objects that can be used. These objects have the exact same API structure as the HttpRequest/HttpResponse objects that you are already familiar with in ASP.NET. The one important difference is that these objects are now interface based instead of sealed classes (specifically: the MVC framework ships with new System.Web.IHttpRequest and System.Web.IHttpResponse interfaces). The benefit of having these be interfaces is that it is now easy to mock them – which enables easy unit testing of controller classes. I'll cover this in more depth in a future blog post.
Below is an example of how we could use the Request API to manually retrieve an ID querystring value from within our Detail action method in the ProductsController class:

The ASP.NET MVC framework also supports automatically mapping incoming URL parameter values as parameter arguments to action methods. By default, if you have a parameter argument on your action method, the MVC framework will look at the incoming request data to see if there is a corresponding HTTP request value with the same name. If there is, it will automatically pass it in as a parameter to your action method.
For example, we could re-write our Detail action method to take advantage of this support and make it cleaner like below:

In addition to mapping argument values from the querystring/form collection of a request, the ASP.NET MVC framework also allows you to use the MVC URL route mapping infrastructure to embed parameter values within the core URL itself (for example: instead of /Products/Detail?id=3 you could instead use /Products/Detail/3).
The default route mapping rule declared when you create a new MVC project is one with the format: “/[controller]/[action]/[id]”. What this means is that if there is any URL sub-path after the controller and action names in the URL, it will by default be treated as a parameter named “id” – and which can be automatically passed into our controller action method as a method argument.
This means that we can now use our Detail method to also handle taking the ID argument from the URL path (e.g: /Products/Detail/3):

太多了,具体访问地址:http://weblogs.asp.net/scottgu/archive/2007/11/13/asp-net-mvc-framework-part-1.aspx
吧
Mikel