原文:返璞归真 asp.net mvc (7) - asp.net mvc 3.0 新特性之 Controller

[索引页]
[源码下载]

返璞归真 asp.net mvc (7) - asp.net mvc 3.0 新特性之 Controller

作者:webabcd

介绍
asp.net mvc 之 asp.net mvc 3.0 新特性之 Controller:

  • Global Action Filter
  • 可以在标记为 ChildActionOnly 的 Action 上使用 OutputCache
  • ViewBag
  • 新增了一些 Action Result

示例
1、Global Action Filter 的 Demo
Global.asax.cs(注册全局的 Action Filter)

  1. protected void Application_Start()
  2. {
  3. AreaRegistration.RegisterAllAreas();
  4. RegisterRoutes(RouteTable.Routes);
  5.  
  6. /*
  7. * 演示 Global Action Filter
  8. */
  9.  
  10. // 实例化一个 Filter
  11. var handleError = new HandleErrorAttribute();
  12. // 指定 HandleErrorAttribute 的 View
  13. handleError.View = "Error2";
  14. // Order 属性的默认值为:-1,即不会被应用,所以这里要修改一下
  15. handleError.Order = ;
  16.  
  17. // 将 Filter 对象添加到全局 Filters 集合中
  18. GlobalFilters.Filters.Add(handleError);
  19. }

Web.config

  1. <system.web>
  2.  
  3. <!--
  4. 如果需要启用 HandleError ,那么要在 web.config 中做如下配置:<customErrors mode="On" />
  5. -->
  6. <customErrors mode="On" />
  7.  
  8. </system.web>

ControllerDemoController.cs

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6.  
  7. namespace MVC30.Controllers
  8. {
  9. public class ControllerDemoController : Controller
  10. {
  11. // 用于演示 Global Action Filter
  12. public ActionResult GlobalActionFilter()
  13. {
  14. throw new Exception("exception");
  15. }
  16. }
  17. }

GlobalActionFilter.cshtml(访问此页会抛出异常,然后跳转到Error2)

  1. @{
  2. ViewBag.Title = "Global Action Filter";
  3. }
  4.  
  5. <h2>GlobalActionFilter</h2>

Error2.cshtml(自定义错误页)

  1. @{
  2. Layout = null;
  3. }
  4.  
  5. <!DOCTYPE html>
  6. <html>
  7. <head>
  8. <title>Error</title>
  9. </head>
  10. <body>
  11. <!--
  12. HTTP 返回 500 时,页面必须输出足够多的信息才会显示,否则只会显示 IE HTTP 500 默认页
  13. -->
  14. <h2>
  15. Sorry, an error occurred while processing your request
  16. </h2>
  17. <h2>
  18. Sorry, an error occurred while processing your request
  19. </h2>
  20. <h2>
  21. Sorry, an error occurred while processing your request
  22. </h2>
  23. <h2>
  24. Sorry, an error occurred while processing your request
  25. </h2>
  26. <h2>
  27. Sorry, an error occurred while processing your request
  28. </h2>
  29. </body>
  30. </html>

2、标记为 ChildActionOnly 的 Action 支持 OutputCache
ControllerDemoController.cs

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6.  
  7. namespace MVC30.Controllers
  8. {
  9. public class ControllerDemoController : Controller
  10. {
  11. public ActionResult ChildActionOnlyDemo()
  12. {
  13. return View();
  14. }
  15.  
  16. // ChildActionOnly - 指定 Action 只能让 RenderAction 调用
  17. // OutputCache() - 缓存。Duration - 缓存秒数。VaryByParam - none, *, 多个参数用逗号隔开。也可以通过配置文件对缓存做设置
  18. [ChildActionOnly]
  19. [OutputCache(Duration = )]
  20. public PartialViewResult _GetCurrentTime()
  21. {
  22. var currentTime = DateTime.Now;
  23.  
  24. return PartialView(currentTime);
  25. }
  26. }
  27. }

_GetCurrentTime.cshtml

  1. @*
  2. 通过 @model 指定 Model 的类型,同时 Model 对象就是 Action 返回的数据
  3. *@
  4.  
  5. @model DateTime
  6.  
  7. <div>
  8. currentTime: @Model.ToString("yyyy-MM-dd HH:mm:ss")
  9. </div>

ChildActionOnlyDemo.cshtml

  1. @{
  2. ViewBag.Title = "可以在标记为 ChildActionOnly 的 Action 上使用 OutputCache";
  3. }
  4.  
  5. <h2>ChildActionOnlyDemo</h2>
  6.  
  7. <div>
  8. @{ Html.RenderAction("_GetCurrentTime"); }
  9. <!--
  10. <% Html.RenderAction("_GetCurrentTime"); %>
  11. -->
  12. </div>
  13.  
  14. <div>
  15. @Html.Action("_GetCurrentTime")
  16. <!--
  17. <%= Html.Action("_GetCurrentTime") %>
  18. -->
  19. </div>
  20.  
  21. <!--
  22. Html.Action Html.RenderAction 的区别:
  23. Html.Action - 直接将 Action 的结果作为一个字符串输出
  24. Html.RenderAction - Action 作为一个用户控件嵌入到当前的 HttpContext
  25. -->

3、 新增了 ViewBag
ControllerDemoController.cs

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6.  
  7. namespace MVC30.Controllers
  8. {
  9. public class ControllerDemoController : Controller
  10. {
  11. // 用于 ViewBagDemo
  12. public ActionResult ViewBagDemo()
  13. {
  14. // ViewBag 的本质就是把 ViewData 包装成为 dynamic 类型
  15. ViewBag.Message = "ViewBag 的 Demo";
  16.  
  17. return View();
  18. }
  19. }
  20. }

ViewBagDemo.cshtml

  1. @{
  2. ViewBag.Title = "ViewBag";
  3. }
  4.  
  5. <h2>ViewBag</h2>
  6.  
  7. Message: @ViewBag.Message

4、 新增的 Action Result

  1. <p>
  2. Controller 中新增了一些 Action Result: HttpNotFoundResult, HttpRedirectResult, HttpStatusCodeResult
  3. </p>

OK
[源码下载]

返璞归真 asp.net mvc (7) - asp.net mvc 3.0 新特性之 Controller的更多相关文章

  1. 返璞归真 asp.net mvc (13) - asp.net mvc 5.0 新特性

    [索引页][源码下载] 返璞归真 asp.net mvc (13) - asp.net mvc 5.0 新特性 作者:webabcd 介绍asp.net mvc 之 asp.net mvc 5.0 新 ...

  2. 返璞归真 asp.net mvc (8) - asp.net mvc 3.0 新特性之 Model

    原文:返璞归真 asp.net mvc (8) - asp.net mvc 3.0 新特性之 Model [索引页][源码下载] 返璞归真 asp.net mvc (8) - asp.net mvc ...

  3. 返璞归真 asp.net mvc (12) - asp.net mvc 4.0 新特性之移动特性

    原文:返璞归真 asp.net mvc (12) - asp.net mvc 4.0 新特性之移动特性 [索引页][源码下载] 返璞归真 asp.net mvc (12) - asp.net mvc ...

  4. 返璞归真 asp.net mvc (11) - asp.net mvc 4.0 新特性之自宿主 Web API, 在 WebForm 中提供 Web API, 通过 Web API 上传文件, .net 4.5 带来的更方便的异步操作

    原文:返璞归真 asp.net mvc (11) - asp.net mvc 4.0 新特性之自宿主 Web API, 在 WebForm 中提供 Web API, 通过 Web API 上传文件, ...

  5. 返璞归真 asp.net mvc (9) - asp.net mvc 3.0 新特性之 View(Razor)

    原文:返璞归真 asp.net mvc (9) - asp.net mvc 3.0 新特性之 View(Razor) [索引页][源码下载] 返璞归真 asp.net mvc (9) - asp.ne ...

  6. 返璞归真 asp.net mvc (10) - asp.net mvc 4.0 新特性之 Web API

    原文:返璞归真 asp.net mvc (10) - asp.net mvc 4.0 新特性之 Web API [索引页][源码下载] 返璞归真 asp.net mvc (10) - asp.net ...

  7. 返璞归真 asp.net mvc (6) - asp.net mvc 2.0 新特性

    原文:返璞归真 asp.net mvc (6) - asp.net mvc 2.0 新特性 [索引页][源码下载] 返璞归真 asp.net mvc (6) - asp.net mvc 2.0 新特性 ...

  8. asp.net mvc 4.0 新特性之移动特性

    asp.net mvc 4.0 新特性之移动特性 为不同的客户端提供不同的视图 手动重写 UserAgent,从而强制使用对应的视图 示例1.演示如何为不同的客户端提供不同的视图Global.asax ...

  9. ASP.NET4.0新特性

    原文:ASP.NET4.0新特性 在以前试用VS2010的时候已经关注到它在Web开发支持上的一些变化了,为此我还专门做了一个ppt,当初是计划在4月12日那天讲的,结果因为莫名其妙的原因导致没有语音 ...

随机推荐

  1. [置顶] 如何在Python IDLE中调试Python代码?

    好久没有用Python了,居然忘记了怎么在Python IDLE中调试Python代码.百度了一下,然后还是写下来吧,以免以后又忘记了. 1. Set break point in the sourc ...

  2. 探索Oracle之数据库升级七 11gR2 to 12c 升级完毕后插入PDB

    探索Oracle之数据库升级七 11gR2 to 12c 升级完毕后插入PDB 前言:        从Oracle 12c開始,引入了容器数据库的概念,能够实现数据库插拔操作,例如以下图: 如今我们 ...

  3. jsoncpp使用

    第一个github网站下载jsoncpp最新的版本库:https://github.com/open-source-parsers/jsoncpp 点击右下角的Download ZIP进行下载 解压后 ...

  4. (二十一)unity4.6学习Ugui中文文档-------交互-Supported Events &amp; Raycasters

    大家好,我是孙广东. 转载请注明出处:http://write.blog.csdn.net/postedit/38922399 更全的内容请看我的游戏蛮牛地址:mod=guide&view=m ...

  5. 单片机实验: 三轴磁场模块 GY-271

    最近买了一块三轴磁场模块进行实验 名称:HMC5883L模块(三轴磁场模块) 型号:GY-271 使用芯片:HMC5883L 供电电源:3-5v 通信方式:IIC通信协议 测量范围:±1.3-8 高斯 ...

  6. SE 2014年5月25日

    如图配置 两实验 R1模拟总部,R2 与R3模拟分部 实验一  要求使用 IPSec VPN 主模式,使得总部与两分部内网可相互通讯 步骤: 1.  配置默认路由 [RT1]ip route-stat ...

  7. jenkins 集成 redmine 账户验证的方案

    jenkins 集成 redmine 账户验证的方案 赖勇浩(http://laiyonghao.com) 动机 Jenkins 是最著名的持续集成工具,又因为它开源免费.插件众多,成为了许多团队做持 ...

  8. 添加PDF文件对照度的粗浅原理,及方法

      上边这张照片不是异形,而是著名的鹦鹉螺.下边这张照片,是送给研究生同学的毕业纪念,向龙同学帮我激光雕刻的. 近期的照片在[http://www.douban.com/photos/album/13 ...

  9. orcl 删除重复的行

    delete from FOODDETAIL t where t.id in (select   t.id from FOODDETAIL where t.sendtime>=to_date(' ...

  10. pygame各个模块概述

    在pygame中,有很多模块,每个模块对应着不同的功能,如果我们知道这些模块是做什么的,那么,对我们的游戏开发会起到关键性的作用. 我们就说说pygame中的各个模块吧!!! #pygame modu ...