返璞归真 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)
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterRoutes(RouteTable.Routes); /*
* 演示 Global Action Filter
*/ // 实例化一个 Filter
var handleError = new HandleErrorAttribute();
// 指定 HandleErrorAttribute 的 View
handleError.View = "Error2";
// Order 属性的默认值为:-1,即不会被应用,所以这里要修改一下
handleError.Order = ; // 将 Filter 对象添加到全局 Filters 集合中
GlobalFilters.Filters.Add(handleError);
}
Web.config
<system.web>
<!--
如果需要启用 HandleError ,那么要在 web.config 中做如下配置:<customErrors mode="On" />
-->
<customErrors mode="On" />
</system.web>
ControllerDemoController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc; namespace MVC30.Controllers
{
public class ControllerDemoController : Controller
{
// 用于演示 Global Action Filter
public ActionResult GlobalActionFilter()
{
throw new Exception("exception");
}
}
}
GlobalActionFilter.cshtml(访问此页会抛出异常,然后跳转到Error2)
@{
ViewBag.Title = "Global Action Filter";
}
<h2>GlobalActionFilter</h2>
Error2.cshtml(自定义错误页)
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<title>Error</title>
</head>
<body>
<!--
HTTP 返回 500 时,页面必须输出足够多的信息才会显示,否则只会显示 IE 的 HTTP 500 默认页
-->
<h2>
Sorry, an error occurred while processing your request
</h2>
<h2>
Sorry, an error occurred while processing your request
</h2>
<h2>
Sorry, an error occurred while processing your request
</h2>
<h2>
Sorry, an error occurred while processing your request
</h2>
<h2>
Sorry, an error occurred while processing your request
</h2>
</body>
</html>
2、标记为 ChildActionOnly 的 Action 支持 OutputCache
ControllerDemoController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc; namespace MVC30.Controllers
{
public class ControllerDemoController : Controller
{
public ActionResult ChildActionOnlyDemo()
{
return View();
} // ChildActionOnly - 指定 Action 只能让 RenderAction 调用
// OutputCache() - 缓存。Duration - 缓存秒数。VaryByParam - none, *, 多个参数用逗号隔开。也可以通过配置文件对缓存做设置
[ChildActionOnly]
[OutputCache(Duration = )]
public PartialViewResult _GetCurrentTime()
{
var currentTime = DateTime.Now; return PartialView(currentTime);
}
}
}
_GetCurrentTime.cshtml
@*
通过 @model 指定 Model 的类型,同时 Model 对象就是 Action 返回的数据
*@ @model DateTime <div>
currentTime: @Model.ToString("yyyy-MM-dd HH:mm:ss")
</div>
ChildActionOnlyDemo.cshtml
@{
ViewBag.Title = "可以在标记为 ChildActionOnly 的 Action 上使用 OutputCache";
}
<h2>ChildActionOnlyDemo</h2>
<div>
@{ Html.RenderAction("_GetCurrentTime"); }
<!--
<% Html.RenderAction("_GetCurrentTime"); %>
-->
</div>
<div>
@Html.Action("_GetCurrentTime")
<!--
<%= Html.Action("_GetCurrentTime") %>
-->
</div>
<!--
Html.Action 与 Html.RenderAction 的区别:
Html.Action - 直接将 Action 的结果作为一个字符串输出
Html.RenderAction - 将 Action 作为一个用户控件嵌入到当前的 HttpContext 中
-->
3、 新增了 ViewBag
ControllerDemoController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc; namespace MVC30.Controllers
{
public class ControllerDemoController : Controller
{
// 用于 ViewBagDemo
public ActionResult ViewBagDemo()
{
// ViewBag 的本质就是把 ViewData 包装成为 dynamic 类型
ViewBag.Message = "ViewBag 的 Demo"; return View();
}
}
}
ViewBagDemo.cshtml
@{
ViewBag.Title = "ViewBag";
}
<h2>ViewBag</h2>
Message: @ViewBag.Message
4、 新增的 Action Result
<p>
Controller 中新增了一些 Action Result: HttpNotFoundResult, HttpRedirectResult, HttpStatusCodeResult
</p>
OK
[源码下载]
返璞归真 asp.net mvc (7) - asp.net mvc 3.0 新特性之 Controller的更多相关文章
- 返璞归真 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 新 ...
- 返璞归真 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 ...
- 返璞归真 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 ...
- 返璞归真 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 上传文件, ...
- 返璞归真 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 ...
- 返璞归真 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 ...
- 返璞归真 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 新特性 ...
- asp.net mvc 4.0 新特性之移动特性
asp.net mvc 4.0 新特性之移动特性 为不同的客户端提供不同的视图 手动重写 UserAgent,从而强制使用对应的视图 示例1.演示如何为不同的客户端提供不同的视图Global.asax ...
- ASP.NET4.0新特性
原文:ASP.NET4.0新特性 在以前试用VS2010的时候已经关注到它在Web开发支持上的一些变化了,为此我还专门做了一个ppt,当初是计划在4月12日那天讲的,结果因为莫名其妙的原因导致没有语音 ...
随机推荐
- memwatch的使用
博主的新Blog地址:http://www.brantchen.com 欢迎訪问:) linux下的測试工具真是少之又少,还不好用,近期试用了memwatch,感觉网上的介绍不太好,所以放在这里跟大家 ...
- LIS(最长的序列)和LCS(最长公共子)总结
LIS(最长递增子序列)和LCS(最长公共子序列)的总结 最长公共子序列(LCS):O(n^2) 两个for循环让两个字符串按位的匹配:i in range(1, len1) j in range(1 ...
- iOS 同步GET
(注意: 能够整片复制)
- hdu 2642 二维树状数组 单点更新区间查询 模板水题
Stars Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/65536 K (Java/Others) Total Subm ...
- Git——git 上传时 遗漏文件解决办法
今天在Server上建立一个git 库,把本地的code 上传到Server,再次clone下来时,发现少了些文件.原来git 工具不上上传一些二进制,pdf,.patch等一些文件.在上传时,git ...
- Android开发系列(二十二):AdapterViewFlipper的功能和使用方法
AdapterViewFlipper继承了AdapterViewAnimator,它会显示一个View组件,能够通过showPrevious()和showNext()方法控制组件显示上一个.下一个组件 ...
- Xenu-web开发死链接检測工具应用
Xenu 是一款深受业界好评,并被广泛使用的死链接检測工具. 时常检測站点并排除死链接,对站点的SEO 很重要,由于大量死链接存在会减少用户和搜索引擎对站点的信任,web程序开发者还可通过其找到死链接 ...
- poj3252(数位dp)
题目连接:http://poj.org/problem?id=3252 题意:拆成2进制,在记录0和1的个数 求区间[a,b]中,满足传化成2进制后,0的个数>=1的个数的数字的个数... 分析 ...
- 利用python 提取log 文件里的关键句子,并进行统计分析
利用python开发了一个提取sim.log 中的各个关键步骤中的时间并进行统计的程序: #!/usr/bin/python2.6 import re,datetime file_name='/hom ...
- 【DRP】删除递归树的操作
正如图呈现的树结构.本文从任意节点删除树形结构.提供解决方案 图中,不包括其他结点的是叶子结点.包括其他结点的是父结点,即不是叶子结点. 一 本文的知识点: (1)递归调用: 由于待删除的结点的层次是 ...