MindMap Gallery ASP.Net MVC 101
This mind map is about ASP.Net MVC 101. Start to use a mind map to express and organize your ideas and knowledge right now.
Edited at 2020-09-28 12:33:18This mind map is about Wholesaling Blueprint - Steps to Wholesaling Real Estate + Simple Systems. Start to use a mind map to express and organize your ideas and knowledge right now.
This mind map is about Western Front. Start to use a mind map to express and organize your ideas and knowledge right now.
This mind map is about THE SAMPLING PROCESS. Start to use a mind map to express and organize your ideas and knowledge right now.
This mind map is about Wholesaling Blueprint - Steps to Wholesaling Real Estate + Simple Systems. Start to use a mind map to express and organize your ideas and knowledge right now.
This mind map is about Western Front. Start to use a mind map to express and organize your ideas and knowledge right now.
This mind map is about THE SAMPLING PROCESS. Start to use a mind map to express and organize your ideas and knowledge right now.
ASP.Net MVC 101
ASP.Net MVC Framework 101 History0.1 Create 2/2/2010 Lanvige0.2 Modify 4/8/2010 Lanvige [Add fiter]0.3 Modify 4/20/2010
URL Routing
MVC 独立于RoutingUrl Routing -> Controller || Action -> Html Renter 2.3 设置路由的扩展方法 40 2.3.1 Route类 40 2.3.2 RouteCollection类 41 2.3.3 MapRoute()扩展方法 43 2.3.4 优化路由设置 44 2.4 自定义路由约束 45 2.4.1 添加自定义路由约束类 45 2.4.2 设置路由 48 2.4.3 运行界面 49 2.5 路由解析分析 50 2.5.1 路由解析管道 50 2.5.2 UrlRoutingModule类 51 2.5.3 IRouteHandler接口 55 2.5.4 IHttpHandler接口 56 2.6 在Web Form项目中使用路由 59 2.6.1 WebFormRouteHandler类 59 2.6.2 配置全局应用程序类 60
Rule
fda
Extented Routing
MVC Role
View和数据(Model)分离 – 能够为相同的Model开发不同的View进行展示 – 分离可视与不可视得组件,能够对Model进行独立测试(只有View到Model的单向引用) •View和表现逻辑(Controller)分离 – 分离逻辑与具体展示,能够对逻辑进行独立测试(只有Controller到View的单向引用)
Model
存储数据的组件
Validation
Model Binding
从Model绑定到View上使用的方法ModelBinder我们就可以大胆的传递各种类型的参数给Action了 http://www.cnblogs.com/niuchenglei/archive/2010/03/12/1684683.html
Validation
ControllerAction
接受并处理用户指令(操作Model) 选择一个View并输出内容。 ControllerController && ActionAction * 根据每每一次Request的URL都都可以确确定: – Controller (类类) – Action (方法) * AActition中执行表现逻辑中执行表现逻辑 – 操作Model,获取数据,选择View,etc. * Action执行之后将返回一个ActionResult – 使用使用ViewView生成生成HTMLHTML – 页面跳转 – etc. Action的要求 * 必须是一个个公有有方法法 * 必须返回必须返回ActionResultActionResult类型类型 * 必须是实例方法 * 不能是范型方法 ** 没有标注没有标注NonActionAttributeNonActionAttribute * 不能被重载(overload)
Receiving Input
QueryString
Request.Form
FormCollection
Custome(IModelBinding)
Async Action
Filter
Level
Controller
对该Controller下的所有Action生效
Action
仅对该Action生效
IActionFilter
在Action 前后执行的,类似于AOPhttp://zengzhijun369.blog.163.com/blog/static/306844372009102554328124/有时候你想在调用action方法之前或者action方法之后处理一些逻辑,为了支持这个,ASP.NET MVC允许你创建action过滤器。Action过滤器是自定义的Attributes,用来标记添加Action方法之前或者Action方法之后的行为到控制器类中的Action方法中。 一些可能用到Action过滤器的地方有:日志,异常处理 身份验证和授权 - 限制用户的访问 输出缓存 - 保存一个Action的结果 网络爬虫的过滤 本地化 动态Action - 将一个Action注入到控制器中 ASP.NET MVC为我们提供了下面的几个Filter接口:IActionFilter IAuthorizationFilter IExceptionFilter IResultFilter
OnActionExcuting
OnActionExcuted
IResultFilter
OnResultExecuting
OnResultExecuted
IAuthorizationFilters
OnAuthorization
IExceptionFilter
OnException
Filter Interface
Order
Default:ABCC -> B -> A Order:1 -> 2 -> 3
ActionFilterAttribute
OutputCacheAttribute
AuthorizeAttribute
ValidateInputAttribute
ValidateAntiforgeryTokenAttribute
HandleErrorAttribute
FilterAttribute
Class hierarchy of ASP.NET MVC’s built-in filter
Producing Output
Action Result
ontroller中的方法 返回对象View ViewResultRedirect RedirectResultRedirectToAction RedirectToActionResultRedirectToRoute RedirectToRouteResultJson JsonResultJavaScriptResult JavaScriptResultContent ContentResultFile FileContentResult FilePathResult FileStreamResult ActionResult * 一个个抽象类象类,表示Action的执执行结结果 – 框架已内置多种框架已内置多种AcctioonResuesult – 可扩展 * ASPASP.NETNET MVCMVC框架将在获得框架将在获得AActitionRResultlt 之后执行其Execute方法 * 在ASP.NET MVC Preview 2及以前的设计中并不存在中并不存在ActionResultActionResult – 可测试性 各种ActionResult * ActionRedirectResult:跳转转到某个个Action * HttpRedirectResultHttpRedirectResult::跳转到任意地址跳转到任意地址 * RenderViewResult:生成内容 * EmptyResult:什么都不做 ViewResult – Represents HTML and markup. 2. EmptyResult – Represents no result. 3. RedirectResult – Represents a redirection to a new URL. 4. JsonResult – Represents a JavaScript Object Notation result that can be used in an AJAX application. 5. JavaScriptResult – Represents a JavaScript script. 6. ContentResult – Represents a text result. 7. FileContentResult – Represents a downloadable file (with the binary content). 8. FilePathResult – Represents a downloadable file (with a path). 9. FileStreamResult – Represents a downloadable file (with a file stream).
ViewResult
表示HTML的页面内容 ActionResult是一个抽象类,ViewResult是他的一个子类 解析并显示某View文件的内容。在[Controller]中使用View 方法调用 。 {{{{public ActionResult Index(){ return View(viewfile,masterfile);//eg. View("index","master1")}}}}}
ContentResult
Content 用于输出(Response.Write) "静态" 片段。protected internal virtual ContentResult Content(content, contentType, contentEncoding){ return new ContentResult { Content = content, ContentType = contentType, ContentEncoding = contentEncoding };} public class ContentResult : ActionResult{ public string Content { get; set; } public override void ExecuteResult(ControllerContext context) { ... HttpResponseBase response = context.HttpContext.Response; if (!String.IsNullOrEmpty(ContentType)) { response.ContentType = ContentType; } if (ContentEncoding != null) { response.ContentEncoding = ContentEncoding; } if (Content != null) { response.Write(Content); } }}
RedirectResult
4. Redirect / RedirectToAction / RedirectToRoute
JsonResult
JavaScriptResult
EmptyResult
FileResult
FileContentResult表示一个可以下载的、二进制内容的文件FilePathResult表示一个可以下载的、指定路径的文件FileStreamResult表示一个可以下载的、流式的文件
FileContentResult
FilePathResult
FileStreamResult
TempData
View Data
PartialView
Controller.PartialView() 和 HtmlHelper.RenderPartial() 的不同之处在于前者是再次执行 ActionInvoke 并返回一个 ActionResult 结果,后者只是使用现有的 ViewContext 显示一个视图片段。而与 Controller.Content() 的区别是 PartialView() 使用视图引擎输出一个 "动态" 的 ascx 结果。
View Model
How defines
End with the word Controller
Inherit from System.Web.Mvc.Controller (or implement IController
Contain public methods that return ActionResult
View
根据Model数据进行内容展示的组件
ViewEngine
配置Html Renter
ViewPage
StrongType
MVC 还提供了强类型的 ViewPage。public class User{ public string Name { get; set; } public int Age { get; set; }} public class TestController : Controller{ public ActionResult Index() { ViewData["message"] = "Hello, World!"; var model = new User { Name = "Tom", Age = 13 }; return View(model); }} Index.aspx Name: ; Age:
View Data
ViewModel
HTML Helper
RenderPartial
RenderPartial 从当前视图目录(Views\xxx)下载入 .ascx 页面 RenderPartialInternal 调用 FindParitialView 从视图引擎中载入 .ascx,同时将当前的环境参数传递给它。也就是说 RenderPartial 只是一种视图级别的行为,并不会再次触发 Controller Action 操作,这点要和 Controller.PartialView() 区别开来。
Using MasterPage
Partial views
Form Posting
Validation and Error Reporting
Customizing & Extending
Extending URL Routing
Custom ControllerFactory
Leveraging IoC for Controllers
Extending the Controller & Action
Custom View Engine
Custom T4 Templates
Core
Introduction
对比Observed pattern
What's ASP.Net MVC
What's ASP.Net MVC Framework
MVC Pattern
Model
A model, which represents the items, operations, and rules that are meaningful in thesubject matter (domain) of your application. The “thing” that your software is built around.
View
A set of views, which describe how to render some portion of the model as a visible UI,but otherwise contain no logic.
Controller
A set of controllers, which handle incoming requests, perform operations on the model,and choose a view to render back to the user
Why ASP.Net MVC
Full control over HTML
Full control over URLs
Better separation of concerns
Extensibility
Form封装性太强,用好难
Testability
Differents
1
REST/POST
Postback
2
Controller
Page and control lifecycle
3
Separate controller
Code behind
MVC Life Cycle
Routing
MvcHandler
ControllerActionInvoker
ViewResult
ControllerContext
ASP.NET MVC 通过层层封装的上下文对象来传递执行流程所需的环境参数。 封装的请求上下文
2.0 Features
Areas
Dynamic Scaffolding
Enhanced Model Validation
Asynchronous Controllers support
rendering sub-sections
Important
System.Webhttp://msdn.microsoft.com/zh-cn/library/system.web.aspx
HttpApplication
定义 ASP.NET 应用程序中的所有应用程序对象共有的方法、属性和事件。此类是用户在 Global.asax 文件中所定义的应用程序的基类。 按照以下顺序引发应用程序事件: BeginRequestAuthenticateRequestPostAuthenticateRequestAuthorizeRequestPostAuthorizeRequestResolveRequestCachePostResolveRequestCache 在 PostResolveRequestCache 事件之后和 PostMapRequestHandler 事件之前,会创建一个事件处理程序(一个对应于请求 URL 的页)。如果服务器在集成模式下运行 IIS 7.0 并且 .NET Framework 至少为 3.0 版本,则会引发 MapRequestHandler 事件。如果服务器在经典模式下运行 IIS 7.0 或者运行的是较早版本的 IIS,则无法处理此事件。 PostMapRequestHandlerAcquireRequestStatePostAcquireRequestStatePreRequestHandlerExecute 执行事件处理程序。 PostRequestHandlerExecuteReleaseRequestStatePostReleaseRequestState 在引发 PostReleaseRequestState 事件之后,现有的所有响应筛选器都将对输出进行筛选。 UpdateRequestCachePostUpdateRequestCacheLogRequest. 仅在 IIS 7.0 处于集成模式并且 .NET Framework 至少为 3.0 版本的情况下才支持此事件。 PostLogRequest 仅在 IIS 7.0 处于集成模式并且 .NET Framework 至少为 3.0 版本的情况下才支持此事件。 EndRequest
Application_Start
Application_BeginRequest
在 ASP.NET 响应请求时作为 HTTP 执行管线链中的第一个事件发生。
HttpContextBase
用作包含有关某个 HTTP 请求的 HTTP 特定信息的类的基类。 HttpContextBase 类为抽象类,该类包含的成员与 HttpContext 类相同。使用 HttpContextBase 类可以创建一些派生类,这些派生类与 HttpContext 类相似,但是可以进行自定义并在 ASP.NET 管道外部使用。在执行单元测试时,通常使用派生类实现具有自定义行为的成员以实现正在测试的方案。 HttpContextWrapper2 类是从 HttpContextBase 类派生的。HttpContextWrapper2 类用作 HttpContext 类的包装。在运行时,通常使用 HttpContextWrapper2 类的实例调用 HttpContext 对象上的成员。
HttpContextWrapper
封装包含有关单个 HTTP 请求的 HTTP 特定信息的 HTTP 内部对象。
HttpContext
Mock HttpContext对象 * System.Web.Abstraction * 提供了HttpContext相关的抽象类及封装类 – virtual everywhere * 可以用于创建HttpContext相关的对象
Q/A
Convention Over Configuration
ASP.NETASP.NET MVCMVC的重要理念的重要理念 * 约定胜于配置 – Convention over Configguration * 配置能够带来 – 扩展性扩展性 – 复杂性 * 很多时候约定依旧足够了 – XxxControllerXxxController – View\ControllerName\Xxx.aspx
MVC and 3 tier architecture
MVC与三层架构类似? – View – UILayer y –Controller–BLLayer Model DA Layer – Model – DA Layer • 其实不然 –MVC是表现模式(Presentation Pattern)三层是协作关系,没有依赖。 –三层架构是架构模式(ArchitecturePattern)依赖是单向的。
Leveraging Existing ASP.NET Features
Practices
Unit test
单元测试 * 确保代确保代码中中的最小单单元运行正常 – 面向对象应用程序的最小单元面向对象应用程序的最小单元::方法方法 * 保证软件开发质量的重要手段 – 重构的基准重构的基准 – 回归测试 * 开发人员负责编写
Principle
单元测试的原则: * 每个单元测试粒度小 出错后迅速定位! * 单元测试之间互相独立 两个方法依赖,出错了,不知是出错在哪! * 被测试组件之间的隔离 – 对业务逻辑层的测试不涉及数据访问层具体实现
routes
controllers
custom model binders
action filters
What to test
Mocks
Mock数据而非行为提供隔离 Rhino Mock框架 * 创建大建大量Mock框架是一件非常繁琐的事情 – 甚至需要为每个单元测试创建Mocock对象 * Mock框架简化Mock对象的创建 * 并可对方法调用进行验证并可对方法调用进行验证 – 测试行试行为,而不仅仅是结果
Moq
Moq http://code.google.com/p/moq/ 大量使用Lambda表达式 微软ASP.NET MVC框架开发团队使用 使用简捷,语义清晰
Lambda available
Rhino.Mocks
Full-featured &
Powerful
Full System Testing
AJAX
jQuery
Microsoft Ajax
Scaling the Architecture
Data Access
Project & Extentions
MVC Contrib
MVC Membership
Oxite 2
NerdDinner
Subtopic 4
Customizing VS for MVC
T4 Templete
Security
Authentication
authorization
AuthorizeAttribute
XSS
Dependency Injection of Controllers
Zhiming Jiang
@lanvige
lanvige@gmail.com