返璞歸真 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
- 可以在標(biāo)記為 ChildActionOnly 的 Action 上使用 OutputCache
- ViewBag
- 新增了一些 Action Result
示例
1、Global Action Filter 的 Demo
Global.asax.cs(注冊(cè)全局的 Action Filter)
protected void Application_Start() { AreaRegistration.RegisterAllAreas(); RegisterRoutes(RouteTable.Routes); /* * 演示 Global Action Filter */ // 實(shí)例化一個(gè) Filter var handleError = new HandleErrorAttribute(); // 指定 HandleErrorAttribute 的 View handleError.View = "Error2"; // Order 屬性的默認(rèn)值為:-1,即不會(huì)被應(yīng)用,所以這里要修改一下 handleError.Order = 0; // 將 Filter 對(duì)象添加到全局 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(訪問(wèn)此頁(yè)會(huì)拋出異常,然后跳轉(zhuǎn)到Error2)
@{ ViewBag.Title = "Global Action Filter"; } <h2>GlobalActionFilter</h2>
Error2.cshtml(自定義錯(cuò)誤頁(yè))
@{ Layout = null; } <!DOCTYPE html> <html> <head> <title>Error</title> </head> <body> <!-- HTTP 返回 500 時(shí),頁(yè)面必須輸出足夠多的信息才會(huì)顯示,否則只會(huì)顯示 IE 的 HTTP 500 默認(rèn)頁(yè) --> <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、標(biāo)記為 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 調(diào)用 // OutputCache() - 緩存。Duration - 緩存秒數(shù)。VaryByParam - none, *, 多個(gè)參數(shù)用逗號(hào)隔開(kāi)。也可以通過(guò)配置文件對(duì)緩存做設(shè)置 [ChildActionOnly] [OutputCache(Duration = 5)] public PartialViewResult _GetCurrentTime() { var currentTime = DateTime.Now; return PartialView(currentTime); } } }
_GetCurrentTime.cshtml
@* 通過(guò) @model 指定 Model 的類型,同時(shí) Model 對(duì)象就是 Action 返回的數(shù)據(jù) *@ @model DateTime <div> currentTime: @Model.ToString("yyyy-MM-dd HH:mm:ss") </div>
ChildActionOnlyDemo.cshtml
@{ ViewBag.Title = "可以在標(biāo)記為 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 的區(qū)別: Html.Action - 直接將 Action 的結(jié)果作為一個(gè)字符串輸出 Html.RenderAction - 將 Action 作為一個(gè)用戶控件嵌入到當(dāng)前的 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 的本質(zhì)就是把 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
[源碼下載]
浙公網(wǎng)安備 33010602011771號(hào)