SlideShare a Scribd company logo
1 of 33
LOGO
ASP.NET MVC3 Preview
Email:mailme.xu@gmail.com
目录
 Asp.net MVC 简介
 页面引擎— Razor
 MVC Route
 Area
 Filter
 Controller/Action/Result
 DataAnnotations Attributes
 模板页
 单元测试
 影响 web 性能的几个因素
 常用 Web 开发工具
 Asp.net MVC4
Asp.net MVC 简介
MVC 是一种软件架构模式
改变使用服务端控件、放弃事件驱动的思维
页面响应生命周期
Razor
一种全新的模板引擎
Asp.net: <script src="<%=Url.Content("~/Scripts/jquery-1.4.4.min.js")%>"
Razor: <script src="@Url.Content("~/Scripts/jquery-1.4.4.min.js")"
注意“ ;”
@MyClass.Member; // 将会导致页面多出一个 ;
代码块
@
{
….
}
Area
对于较大的项目比较适用
注册路由
Global.asax.cs 文件中的 RegisterRoutes
路由注册方法重载
 MapRoute(this RouteCollection routes, string name, string url)
 MapRoute(this RouteCollection routes, string name, string url)
 MapRoute(this RouteCollection routes, string name, string url,
object defaults)
 MapRoute(this RouteCollection routes, string name, string url,
string[] namespaces)
 MapRoute(this RouteCollection routes, string name, string url,
object defaults, object constraints)
 MapRoute(this RouteCollection routes, string name, string url,
object defaults, string[] namespaces)
 MapRoute(this RouteCollection routes, string name, string url,
object defaults, object constraints, string[] namespaces)
固定长度的路由
{controller}/{action}
XYZ{controller}/{action}
{controller}/{action}/{id}
{controller}/{action}/{year}-{month}
变长路由
{controller}/{action}/{id}/{*catchall}
如: http://testsite.com/Test/Demo/List/NameId/ProfId
controller = Test, action = Demo, id = list, catch =
NameId/ ProfId
constraints
1. routes.MapRoute(
"Search",
"{id}",
new { controller = "Home", action = "Search" },
new { id = @"d+" }
2. routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
null,
new { controller = @"d{4}" , httpMethod = new
HttpMethodConstraint( "GET", "POST" ) }
);
命名空间
 Controllers 文件夹下的所有子文件夹会被忽略 , 如建立
Admin 和 QA 文件夹,然后分别建立一个
HomeController, 此时在查找到 HomeController 时候,系
统会报错,因为它无法决定使用哪一个
 命名空间的作用是仅在指定的命名空间下反射查找该
controller
使用示例:
routes.MapRoute("routeName1",
"{controller}/{action}",
null,
null,
new string[]
{ "MyApplicationName.SubNamespace" })
练习 1
1. routes.MapRoute("CustomRoute ", "{controller}/
{action}", new { controller = "Home", action
="Index" })
URL Match ?
http://testsite.com/
http://testsite.com/Home
http://testsite.com/Test/Demo
http://testsite.com//Demo
http://testsite.com/Test/Demo/List
Y
Y
Y
N
N
练习 2
public static void RegisterRoutes(RouteCollection routes)
{
....
routes.MapRoute("route1","{controller}/{action}/
{para1}");
routes.MapRoute("route1", "product/view/{para1}");
.....
}
上边代码片段存在什么问题?
取得 Request 中的变量值
RouteData.Values["id"]
public ViewResult CustomVariable(string id) – 参
数名 --- 注意变量名必须一致
如果变量值较多,可以定义一个 ViewModel ,然
后做为参数来使用,注意变量名必须一致。
Route 测试工具
RouteDebugger
Filter
 AOP 简介
 MVC 中的切面 (aspect)
1. Before Action
Public ActionResule ActionMethod()
{
//My Business Code
……
2. After Action
3. Before result
return View();
}
4. After Result
系统提供的一些 Filter
Filter Typ
e
实现接口 执行时间 Default Implementation
Authorizatio
n filter
IAuthorizatio
nFilter
在所有 Filter 和
Action 执行之前
执行
AuthorizeAttribute
Action filter IActionFilter 分别在 Action 执行
之前和之后执行
。
ActionFilterAttribute
Result filter IResultFilter 分别在
Action Result 执
行之后和之前
ResultFilterAttribute
Exception filt
er
IExceptionFil
ter
只有在 filter, HandleErrorAttribute
或
者  action meth
od, 或
者  action result
抛出一个异常时
候执行
Gloable Filter
如果想要为所有的 controller 添加同样的 Filter ,
可以分别为每个 controller 上边添加 attribute ,
更好的做法是使用 global filter.
问题
试举一些 web 开发中的需求可以使用 Filter 来完
成的
系统提供的 ActionResult
类名 功能
ActionResult (Abstract class) 顶层父类
ContentResult 根据内容的类型和编码 , 数据内容 . 通过 Controller 的 Content 方法返回
EmptyResult 返回空结果
FileResult (Abstract class) 写入文件内容 , 具体的写入方式在派生类中 .
FileContentResult 通过 文件 byte[] 写入 Response 返回客户端, Controller 的 File 方法
FilePathResult 通过 文件路径 写入 Response 返回客户端, Controller 的 File 方法
FileStreamResult 通过 Stream 写入 Response 返回客户端, Controller 的 File 方法
HttpUnauthorizedResult 抛出 401 错误
JavaScriptResult 返回 javascript 文件
JsonResult 返回 Json 格式的数据
RedirectResult 使用 Response.Redirect 重定向页面
RedirectToRouteResult 根据 Route 规则重定向页面
ViewResultBase (Abstract class) 调用 IView.Render() 返回视图
PartialViewResult 调用父类 ViewResultBase 的 ExecuteResult 方法 .
重写了父类的 FindView 方法 .
寻找用户控件 .ascx 文件
ViewResult 调用父类 ViewResultBase 的 ExecuteResult 方法 .
重写了父类的 FindView 方法 .
调用父类 ViewResultBase 的 ExecuteResult 方法 .
Controller 的 View() 方法默认封装 ViewResult 返回结果
Action
默认情况下 ,Controller 内的所有 public 方法都为
action, 如不希望成为 action, 可以使用
[NonAction] 标记。
在 action 中不要使用 HttpRequest 类型,而应该
使用 Request ( HttpRequestBase )
action 方法里面不允许有 ref 或 out 型的参数,这
个编译时不会报错,但是运行会抛异常
DataAnnotations Attributes
系统自带的一些验证标签
RangeAttribute
RegularExpressionAttribute
RequiredAttribute
StringLengthAttribute
CompareAttribute
…..
http://msdn.microsoft.com/en-
us/library/system.componentmodel.dataannotati
ons(v=vs.100).aspx
模板页
如果没有在 cshtml 中指定 Layout, 则默认使用
_ViewStart.cshtml 中定义的模板页
内置方法
RenderPage(string path, params object[] data)
RenderBody()
RenderSection(string name)
RenderSection(string name, bool required)
设置默认内容
注意容易出错
Action 中值的传递
ViewData
TempData
ViewBag
Web 开发中的一些第三方组件
 Combres
http://combres.codeplex.com/documentation
 CSSSprites
http://aspnet.codeplex.com/releases/view/65787
 SignalR
https://github.com/SignalR/SignalR
 Jquery,ExtJs,JuqeryUI,JqueryDatatable(http://www.dat
atables.net/ )
 Antixss
http://wpl.codeplex.com/
常用 Web 开发工具
Fiddler
Chrome/Firefox(Firebug)/IE8
IIS Express
Asp.net MVC4
RTM 版本于 2012/08/15 正式发布
推荐资源
 Pro ASP.NET MVC 3 Framework 3rd Edition
http://ishare.iask.sina.com.cn/f/23501178.html
 creating views with MvcContrib.FluentHtml
http://www.arrangeactassert.com/asp-net-mvc-view-best-
practices-save-time-creating-views-with-mvccontrib-
fluenthtml/

More Related Content

Similar to Mvc training

Real World ASP.NET MVC
Real World ASP.NET MVCReal World ASP.NET MVC
Real World ASP.NET MVCjeffz
 
Struts Mitac(1)
Struts Mitac(1)Struts Mitac(1)
Struts Mitac(1)wangjiaz
 
twMVC#01 | ASP.NET MVC 的第一次親密接觸
twMVC#01 | ASP.NET MVC 的第一次親密接觸twMVC#01 | ASP.NET MVC 的第一次親密接觸
twMVC#01 | ASP.NET MVC 的第一次親密接觸twMVC
 
Struts学习笔记
Struts学习笔记Struts学习笔记
Struts学习笔记yiditushe
 
Asp.net mvc網站的從無到有
Asp.net mvc網站的從無到有Asp.net mvc網站的從無到有
Asp.net mvc網站的從無到有Wade Huang
 
Struts快速学习指南
Struts快速学习指南Struts快速学习指南
Struts快速学习指南yiditushe
 
Struts+Spring+Hibernate整合教程
Struts+Spring+Hibernate整合教程Struts+Spring+Hibernate整合教程
Struts+Spring+Hibernate整合教程yiditushe
 
Struts+Spring+Hibernate整合教程
Struts+Spring+Hibernate整合教程Struts+Spring+Hibernate整合教程
Struts+Spring+Hibernate整合教程appollo0312
 
通过Struts构建Web应用
通过Struts构建Web应用通过Struts构建Web应用
通过Struts构建Web应用yiditushe
 
網站設計100步
網站設計100步網站設計100步
網站設計100步evercislide
 
张所勇:前端开发工具推荐
张所勇:前端开发工具推荐张所勇:前端开发工具推荐
张所勇:前端开发工具推荐zhangsuoyong
 
2011 JavaTwo JSF 2.0
2011 JavaTwo JSF 2.02011 JavaTwo JSF 2.0
2011 JavaTwo JSF 2.0Anthony Chen
 
网易 李弈远 网易服务集成框架的构建与运维
网易 李弈远 网易服务集成框架的构建与运维网易 李弈远 网易服务集成框架的构建与运维
网易 李弈远 网易服务集成框架的构建与运维guiyingshenxia
 
网易 李弈远 网易服务集成框架的构建与运维
网易 李弈远 网易服务集成框架的构建与运维网易 李弈远 网易服务集成框架的构建与运维
网易 李弈远 网易服务集成框架的构建与运维colderboy17
 
Performance Monitoring With AOP
Performance Monitoring With AOPPerformance Monitoring With AOP
Performance Monitoring With AOPivannotes
 
+++º+ ¦¦ ¦ ¦¦ ¦+ =
+++º+ ¦¦  ¦ ¦¦ ¦+ =+++º+ ¦¦  ¦ ¦¦ ¦+ =
+++º+ ¦¦ ¦ ¦¦ ¦+ =guesta6295f3
 
J2ee经典学习笔记
J2ee经典学习笔记J2ee经典学习笔记
J2ee经典学习笔记yiditushe
 
Flex 4.5 action custom component development
Flex 4.5 action custom component developmentFlex 4.5 action custom component development
Flex 4.5 action custom component developmentjexchan
 
ASP.Net MVC2 简介
ASP.Net MVC2 简介ASP.Net MVC2 简介
ASP.Net MVC2 简介Allen Lsy
 

Similar to Mvc training (20)

Real World ASP.NET MVC
Real World ASP.NET MVCReal World ASP.NET MVC
Real World ASP.NET MVC
 
Struts Mitac(1)
Struts Mitac(1)Struts Mitac(1)
Struts Mitac(1)
 
twMVC#01 | ASP.NET MVC 的第一次親密接觸
twMVC#01 | ASP.NET MVC 的第一次親密接觸twMVC#01 | ASP.NET MVC 的第一次親密接觸
twMVC#01 | ASP.NET MVC 的第一次親密接觸
 
Struts学习笔记
Struts学习笔记Struts学习笔记
Struts学习笔记
 
敦群學院-SharePoint精英計畫-系統開發-Day 3
敦群學院-SharePoint精英計畫-系統開發-Day 3敦群學院-SharePoint精英計畫-系統開發-Day 3
敦群學院-SharePoint精英計畫-系統開發-Day 3
 
Asp.net mvc網站的從無到有
Asp.net mvc網站的從無到有Asp.net mvc網站的從無到有
Asp.net mvc網站的從無到有
 
Struts快速学习指南
Struts快速学习指南Struts快速学习指南
Struts快速学习指南
 
Struts+Spring+Hibernate整合教程
Struts+Spring+Hibernate整合教程Struts+Spring+Hibernate整合教程
Struts+Spring+Hibernate整合教程
 
Struts+Spring+Hibernate整合教程
Struts+Spring+Hibernate整合教程Struts+Spring+Hibernate整合教程
Struts+Spring+Hibernate整合教程
 
通过Struts构建Web应用
通过Struts构建Web应用通过Struts构建Web应用
通过Struts构建Web应用
 
網站設計100步
網站設計100步網站設計100步
網站設計100步
 
张所勇:前端开发工具推荐
张所勇:前端开发工具推荐张所勇:前端开发工具推荐
张所勇:前端开发工具推荐
 
2011 JavaTwo JSF 2.0
2011 JavaTwo JSF 2.02011 JavaTwo JSF 2.0
2011 JavaTwo JSF 2.0
 
网易 李弈远 网易服务集成框架的构建与运维
网易 李弈远 网易服务集成框架的构建与运维网易 李弈远 网易服务集成框架的构建与运维
网易 李弈远 网易服务集成框架的构建与运维
 
网易 李弈远 网易服务集成框架的构建与运维
网易 李弈远 网易服务集成框架的构建与运维网易 李弈远 网易服务集成框架的构建与运维
网易 李弈远 网易服务集成框架的构建与运维
 
Performance Monitoring With AOP
Performance Monitoring With AOPPerformance Monitoring With AOP
Performance Monitoring With AOP
 
+++º+ ¦¦ ¦ ¦¦ ¦+ =
+++º+ ¦¦  ¦ ¦¦ ¦+ =+++º+ ¦¦  ¦ ¦¦ ¦+ =
+++º+ ¦¦ ¦ ¦¦ ¦+ =
 
J2ee经典学习笔记
J2ee经典学习笔记J2ee经典学习笔记
J2ee经典学习笔记
 
Flex 4.5 action custom component development
Flex 4.5 action custom component developmentFlex 4.5 action custom component development
Flex 4.5 action custom component development
 
ASP.Net MVC2 简介
ASP.Net MVC2 简介ASP.Net MVC2 简介
ASP.Net MVC2 简介
 

Mvc training

Editor's Notes

  1. 需要注意 MVC 并不是很多人理解上的三层架构 , 3-tier or N-tier is : WebPage -- BLL – DAL, Monorail Castle Project 是另外一个第三方的 asp.net MVC 实现 优点: 将不同的功能作更明确地切割,让不同专长的技术人员各司其职,也顺便提高了代码的可读性,便于测试 并达到「松散耦合 (loosely coupled) 」,让组件易于更换和重复使用 ASP.NET 程序员必须先改变过去,把很多业务逻辑、输入验证、页面切换…等杂七杂八的功能,全部写在 Code-Behind (aspx.cs) 里的旧习惯
  2. 1. Step 1 – The RouteTable is Created ( 仅调用一次 ) An application’s Route Table is represented by the static RouteTable.Routes property.  2. Step 2 – The UrlRoutingModule Intercepts the Request This second step happens whenever you make a request. The UrlRoutingModule intercepts every request and creates and executes the right handler. 3. Step 3 – The MvcHandler Executes The MvcHandler creates a controller, passes the controller a ControllerContext, and executes the controller. 4. Step 4 – The Controller Executes The controller determines which controller method to execute, builds a list of parameters, and executes the method. 5. Step 5 – The RenderView Method is Called Typically, a controller method calls RenderView() to render content back to the browser. The Controller.RenderView() method delegates its work to a particular ViewEngine.
  3. 需要注意如果一个 Url 符合多个 Map 规则,则选择注册最早的那个 ( 换句话说, RegisterRoutes 代码中越靠上,优先级越高 ) 默认值可以使用 RouteValueDictionary 或者匿名类型来传输
  4. 有 default 值的时候看起来路由长度是不一样,但还是可以把它归为固定长度一类
  5. localhost/Home/3456 符合 localhost/Home/3456abc 不符合
  6. 如权限验证、日志、防止爬虫、盗链、本地化与国际化、限制上传文件大小
  7. 注意这几个新类型 HttpRequestBase HttpResponseBase HttpContextBase 做 UT 时,可以使用任意 Mock 工具 Mock 出 HttpContextBase 实例,然后通过 controller.ControllerContext 来使用该 mock 对象
  8. If you return PartialView() from your controllers (instead of return View()), then_viewstart.cshtml will not be executed
  9. ViewData 仅仅是在本 acion 及 view 中有效,使用的是键值对的访问方式 TempData 内容存储于 session 之中,一旦被读取就会自动被删除,一般仅用于向下一次 httpreuquest 中传值时候使用 ViewBag 是 dynamic 类型的,使用起来更方便