[Index page]
[Download source code]
Asp.net mvc (7)-asp.net mvc 3.0 Controller
Author: webabcd
Introduction
Asp.net mvc the new features of asp.net mvc 3.0 Controller:
- Global Action Filter
- You can use OutputCache on the Action marked as ChildActionOnly.
- ViewBag
- Added some Action results.
Example
1. Demo of Global Action Filter
Global. asax. cs (register Global Action Filter)
Protected void Application_Start () {AreaRegistration. registerAllAreas (); RegisterRoutes (RouteTable. routes);/** demonstrate Global Action Filter * // instantiate a Filter var handleError = new HandleErrorAttribute (); // specify the View handleError of HandleErrorAttribute. view = "Error2"; // The default value of the Order attribute is-1, which is not applied. Therefore, modify handleError here. order = 0; // Add the Filter object to GlobalFilters in the global Filters set. filters. add (handleError );}
Web. config
<System. web> <! -- To enable HandleError. <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 {// used to demonstrate Global Action Filter public ActionResult GlobalActionFilter () {throw new Exception ("exception ");}}}
GlobalActionFilter. cshtml (an exception is thrown when you access this page, and then jump to Error2)
@{ ViewBag.Title = "Global Action Filter";}
Error2.cshtml (custom error page)
@ {Layout = null ;}<! DOCTYPE html>
2. The Action marked as ChildActionOnly supports 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-specifies that Action can only be called by RenderAction // OutputCache () -cache. Duration-Cache seconds. VaryByParam-none, *. Multiple parameters are separated by commas. You can also set [ChildActionOnly] [OutputCache (Duration = 5)] public PartialViewResult _ GetCurrentTime () {var currentTime = DateTime through the configuration file. now; return PartialView (currentTime );}}}
_ GetCurrentTime. cshtml
@ * Specify the model type through @ Model, and the Model object is the data returned by the Action * @ model DateTime <div> currentTime: @ Model. toString ("yyyy-MM-dd HH: mm: ss") </div>
ChildActionOnlyDemo. cshtml
@ {ViewBag. title = "OutputCache can be used on actions marked as ChildActionOnly";}
3. Added 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 {// the essence of ViewBagDemo public ActionResult ViewBagDemo () {// ViewBag is to package ViewData into a dynamic ViewBag. message = "ViewBag Demo"; return View ();}}}
ViewBagDemo. cshtml
@{ ViewBag.Title = "ViewBag";}
4. Newly Added Action Result
<P> some Action results are added to the Controller: HttpNotFoundResult, HttpRedirectResult, and HttpStatusCodeResult. </p>
OK
[Download source code]