[索引頁]
[源碼下載]
返璞歸真 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 = 0; // 將 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 = 5)] 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
[源碼下載]