首页 技术 正文
技术 2022年11月15日
0 收藏 777 点赞 2,700 浏览 3302 个字

原文:返璞归真 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
[源码下载]

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,960
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,484
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,330
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,113
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,745
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,779